Detailed Description
The embodiment of the invention provides a method, a device and a storage medium for realizing single-instance operation of a client, which are used for realizing that only one single instance is operated in the whole situation of the client and conveniently calling an opened client window.
In order to make the objects, features and advantages of the present invention more obvious and understandable, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is obvious that the embodiments described below are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The first embodiment is as follows:
referring to fig. 1, a flow diagram of a method for calling a singleton interface according to an embodiment of the present invention includes the following steps:
s101, after a preset client is started for the first time, locking a local file corresponding to the preset client, and creating a local server object;
the predetermined client refers to an application program installed on the user equipment, such as a fish fighting APP client. The starting refers to a process that a user opens an application program by clicking an exe executable file or a desktop shortcut start icon. When an application is started, a series of tasks are executed, such as data initialization, obtaining a running environment, connecting a network to initiate a network request, and the like, and the application starting process can be generally managed by creating a single instance.
After the local file corresponding to the preset client is installed on the client, the local file is stored in an application file on a system hard disk, the local files comprise configuration files, plug-ins, executable files and the like, and the locking operation is to add file locks to the local files, so that the local files can only be accessed by the current program, namely the current single instance.
The local server, that is, a server created locally, may be used for process communication, for example, a server based on a local socket (socket) implemented by a QLocalServer, and the QLocalServer is used for creating the local server and may receive a connection from the local socket. The local server object is used for processing communication messages with the client.
Optionally, a local file name and a current process session ID corresponding to the predetermined client are obtained, and the local file name and the current process session ID are spliced to serve as the unique ID of the predetermined client.
Further, the file lock of the local server object and the local file is identified by the predetermined client unique ID, and a Listen function is invoked to Listen to the predetermined client unique ID.
S102, when the preset client is monitored to be started again, after the current start is confirmed to be the second start, sending a message to the local server object, and exiting the current start process;
the restart means that the client is started again when the client is still in the running process after the first start. The client side can create an instance management starting process every time the client side is started, the client side is frequently started, the access data are prone to be disordered, the client side is prevented from being repeatedly started only through the mutual exclusion lock, the client side can not be subjected to any reaction when the client side is opened for the second time after a user is minimized to a tray, when the user is in a tray, the client side to be opened is difficult to find, when the application programs in the tray of the system are too many, the client side is directly started, no reaction is generated, and therefore the user use experience can be directly influenced.
The current single-instance starting process firstly accesses a local file to judge whether the local file is added with a file lock or not, when the fact that the local file cannot be loaded is found, whether the current starting is the second starting or not is confirmed, and since a client possibly exits from an operation state after being started each time, the current starting state needs to be confirmed, specifically, a local file handle corresponding to the preset client is obtained, and whether the local file handle is locked or not is judged; and when the local file handle is not locked, confirming that the current start is the second start. The file handle is used to uniquely identify a local file, and may be represented by the predetermined client unique ID.
And S103, after receiving the message, the local server object displays a main program window on a system interface.
The current starting is confirmed to be secondary starting by communicating with the local server object, the current starting process is exited, and the local server object can control the display of the main program window after successfully receiving the message.
The main program window is the current display window of the client after the client is started for the first time. The currently launched process may notify the presentation of the main window by communicating with the process of the local server.
Optionally, a local connection object is created, the local connection object calls a connectToServer function to connect to the local server object, and after the connection is successful, a predetermined message is sent to the local server object. The predetermined message may be a message for verifying whether the communication is successful, or may be a message for performing a predetermined operation, such as calling back a client host interface.
In the steps, the single-instance operation of the client is ensured by locking the local file of the client, the starting process is quitted after the current secondary starting is confirmed, the data access error caused by starting the client for multiple times is prevented, and the process communication is realized by establishing the local server, so that a user can call and display the main window after the first starting when the secondary starting is carried out.
Example two:
on the basis of fig. 1, a detailed method and a detailed process for implementing the single-instance operation of the client are described in detail with reference to fig. 2, as follows:
s201, creating a local file lock of a client;
before creating a file lock for a local file, a unique ID of the client needs to be set first for identifying the client.
Illustratively, in a client initialization interface, an applicationfile path function is called to obtain a local file name of the client, the function can obtain a file name under a local file path of the client, and a ProcessIdToSessionId function is called to obtain a fixed ID string, the function can obtain a session ID under a current process, the local file name and the ID string of the client are spliced as a unique ID of the predetermined client, and the unique ID is used as a local file name corresponding to a local server name and a file lock and is used for uniquely identifying the local file lock and the local server, so that malicious modification of a subsequent process is prevented.
And creating a QtLockedFile object which is mainly used for controlling the starting of single instances of a plurality of clients. In the object, a setFileName function is called to set the unique ID of the client obtained in the first step as a local file name;
in the object, an open function is called to open a local file handle of the client, and then a lock function is called to remove the file handle and lock. Therefore, the current locked file can only be accessed by the current single instance, and the single instance of the client started for the second time can not acquire the access authority of the local file handle.
And defining and calling an isLock function in the QtLockedFile object, and judging whether the file handle of the client is locked before restarting so as to determine whether the current starting is the second starting.
By calling a file locking method defined in the QtLockedFile object and judging whether the file is locked or not, the local file of the client terminal started for the first time can be locked, and meanwhile, whether the file is started for the first time or not can be judged. Specifically, each time the client is started, the isLock function is called to determine whether the client is started twice. Since it is possible to exit the client after each start of the client, i.e. the travel is finished, it is first necessary to confirm the current client start status.
S202, creating a local server;
the local server can be a virtual server in communication of two processes in programming, such as a local server commonly used in Socket.
Before creating the local server, the client is preferably confirmed to be started for the first time.
If the current starting is the first starting, besides the lock locking is called, a QLocalServer local server object is created, and a list function is called to monitor the unique ID of the client. The QLocalServer object is used for communicating with the starting process opened for the second time.
S203, confirming that the starting is carried out for the second time, and exiting the current starting process;
the application executes the starting process through the single instance, and then runs the state after the starting is finished. In the embodiment of the invention, when the method is applied to the automatic process, whether the current starting is the secondary starting or not is preferentially judged.
If the client is started for the second time, a sendMessage function is called to send a message to the client started for the first time, and then return is called to exit the current starting process.
Optionally, before sending the message, the method further includes: a qlocalcocket local connection object is created and then the connectToServer function is called to connect to the local server object created in S202. The qlocalcocket object may communicate with the first time the client is opened.
And S204, communicating with a local server, and calling and displaying a main program window.
The starting process of the client can realize message transmission among processes by establishing communication. In order to ensure the single-instance operation of the client, the communication between the starting processes can confirm that the current process is the secondary starting, and can transmit parameters, so that the calling display of a main program window can be realized when the secondary starting is carried out, and the main program window can be the display window of the client when the client is started for the first time.
Based on the communication connection in step S203, the local server receives the message sent by the second start process, which indicates that the current communication is successful, and calls an activateWindow function to display the main program window of the client through the communication connection, where the activateWindow function may set the main program window as a top-layer window to display the opened client window interface.
In the embodiment of the invention, the communication mechanism between the clients is started twice based on the clients, so that not only can the single-instance operation of the clients be ensured, but also the opened client windows can be displayed.
It should be understood that, the sequence numbers of the steps in the foregoing embodiments do not imply an execution sequence, and the execution sequence of each process should be determined by its function and inherent logic, and should not constitute any limitation to the implementation process of the embodiments of the present invention.
Example three:
the foregoing mainly describes a method for implementing client-side single-instance operation, and a device for implementing client-side single-instance operation is described in detail below.
Fig. 3 shows a schematic structural diagram of an apparatus for implementing client single-instance operation, including:
the locking module 310 is configured to, after a predetermined client is started for the first time, perform a locking operation on a local file corresponding to the predetermined client, and create a local server object;
after the local file corresponding to the preset client is installed on the client, the local file is stored in an application file on a system hard disk, the local files comprise configuration files, plug-ins, executable files and the like, and the locking operation is to add file locks to the local files, so that the local files can only be accessed by the current program, namely the current single instance.
The local server, i.e., a server created locally, may be used for process communication, such as a local socket (socket) based server implemented by QtLocalServer. The local server object is used for processing communication messages with the client.
Optionally, after the predetermined client is started for the first time, performing a locking operation on the local file corresponding to the client, and creating a local server object further includes:
and acquiring a local file name and a current process session ID corresponding to the preset client, and splicing the local file name and the current process session ID as the unique ID of the preset client.
Optionally, the file lock of the local server object and the local file is identified by using the predetermined client unique ID, and a Listen function is invoked to monitor the predetermined client unique ID.
A confirming module 320, configured to send a message to the local server object and exit from a current starting process after it is monitored that the predetermined client is restarted and it is confirmed that the current starting is the second starting;
optionally, the confirming module 320 includes:
the judging unit is used for acquiring a local file handle corresponding to the preset client and judging whether the local file handle is locked or not;
and the confirming unit is used for confirming that the current start is the second start when the local file handle is not locked.
And the display module 330 is configured to display the main program window on a system interface after the local server object receives the message.
Optionally, the sending the message to the local server object specifically includes:
and creating a local connection object, calling a connectToServer function to connect the local server object by the local connection object, and sending a predetermined message to the local server object after the connection is successful.
The device can ensure the single instance running started by the client through the locking module and the confirmation module, saves system resources, prevents the access data from being confused and making mistakes, realizes process communication and is convenient to display the opened client.
Example four:
fig. 4 is a schematic diagram of a device structure for implementing single-instance operation of a client according to an embodiment of the present invention. The apparatus may generally be a terminal device, i.e. a mobile computer device with a touch screen, including but not limited to a smart phone, a smart watch, a notebook, a tablet, a POS machine, and even a vehicle-mounted computer. As shown in fig. 4, the terminal apparatus 4 of this embodiment includes: a memory 410, a processor 420, and a system bus 430, the memory 410 including executable programs 4101 stored thereon, it being understood by those skilled in the art that the terminal device structure shown in fig. 4 does not constitute a limitation of a server, and may include more or fewer components than shown, or some components in combination, or a different arrangement of components.
The following specifically describes each constituent component of the terminal apparatus with reference to fig. 4:
the memory 410 may be used to store software programs and modules, and the processor 420 performs various functional applications of the terminal and data processing by operating the software programs and modules stored in the memory 410. The memory 410 may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required by at least one function (such as a sound playing function, an image playing function, etc.), and the like; the storage data area may store data (such as audio data, a phonebook, etc.) created according to the use of the server, and the like. Further, the memory 410 may include high speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid state storage device.
On the memory 410 is embodied an executable program 4101 of a network request method, said executable program 4101 may be divided into one or more modules/units, which are stored in said memory 410 and executed by the processor 420 to implement single instance execution of a client, said one or more modules/units may be a series of computer program instruction segments capable of performing specific functions, which are used to describe the execution process of said computer program 4101 in said server 4. For example, the computer program 4101 may be divided into a locking module, a confirmation module, and a display module.
The processor 420 is a control center of the server, connects various parts of the entire terminal device using various interfaces and lines, performs various functions of the terminal and processes data by operating or executing software programs and/or modules stored in the memory 410 and calling data stored in the memory 410, thereby integrally monitoring the terminal. Alternatively, processor 420 may include one or more processing units; preferably, the processor 420 may integrate an application processor, which mainly handles operating systems, application programs, etc., and a modem processor, which mainly handles wireless communications. It will be appreciated that the modem processor described above may not be integrated into processor 420.
The system bus 430 is used to connect functional units inside the computer, and can transmit data information, address information, and control information, and may be, for example, a PCI bus, an ISA bus, a VESA bus, etc. The instructions of the processor 420 are transmitted to the memory 410 through the bus, the memory 410 feeds data back to the processor 420, and the system bus 430 is responsible for data and instruction interaction between the processor 420 and the memory 410. Of course, the system bus 430 may also access other devices such as network interfaces, display devices, and the like.
The terminal device at least comprises a network card, an output device and the like, and other components are not described in detail herein.
In this embodiment of the present invention, the executable program executed by the processor 420 included in the terminal device specifically includes:
a method for realizing single-instance operation of a client comprises the following steps:
after a preset client is started for the first time, locking a local file corresponding to the preset client, and creating a local server object;
when the preset client is monitored to be started again, after the current start is confirmed to be the second start, sending a message to the local server object, and exiting the current start process;
and after receiving the message, the local server object displays a main program window on a system interface.
Further, after the predetermined client is started for the first time, performing a locking operation on the local file corresponding to the client, and creating a local server object further includes:
and acquiring a local file name and a current process session ID corresponding to the preset client, and splicing the local file name and the current process session ID as the unique ID of the preset client.
Further, the splicing the local file name and the current process session ID as the predetermined client unique ID further includes:
and identifying the file lock of the local server object and the local file by using the predetermined client unique ID, and calling a Listen function to monitor the predetermined client unique ID.
Further, the determining that the current start is the second start specifically includes:
acquiring a local file handle corresponding to the preset client, and judging whether the local file handle is locked or not;
and when the local file handle is not locked, confirming that the current start is the second start.
Further, the sending the message to the local server object specifically includes:
and creating a local connection object, calling a connectToServer function to connect the local server object by the local connection object, and sending a predetermined message to the local server object after the connection is successful.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
Those of ordinary skill in the art would appreciate that the modules, elements, and/or method steps of the various embodiments described in connection with the embodiments disclosed herein may be implemented as electronic hardware, or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present invention.
In the several embodiments provided in the present application, it should be understood that the disclosed system, apparatus and method may be implemented in other manners. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the units is only one logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present invention, and not for limiting the same; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.