Control of the number of procedures running instance - Daquan

xiaoxiao2021-03-06  48

Control of the number of procedures running - Daquan Published: April 5, 2004: 9cbs already have 1140 readers read this article often encountered someone to ensure that only one instance is only running, I am also broken. Two or three methods have been given today, I have a big summary today, I hope that everyone will help when they are designed. A program only runs an instance (or limit instance) can usually use the following methods: 1) FindWindow By looking for window titles to determine if the previous instance is running, not suitable for window title dynamic changes. 2) FindWindow By looking for the taskbar to press the button on whether the previous instance is running, it is not suitable for dynamic changes in the new title (such as WINAMP). Normally, the method is still prioritized because pressing the title is generally fixed. 3) WINDOW Property will add a data (can be a string or handle) to the Property List of the specified window through SetProp, the program runtime enumeration window and check if the data exists to determine if the previous instance is running. 4) Global ATOM adds a particular string to the global atomic table (Global Atom table), and the program is running when the program is running to determine if the previous instance is running. This method has a limitations, which must explicitly call GlobalDeleTeatom before the program is terminated to release the Atom, otherwise the atom will not be released automatically. If the program is unexpected, the next instance cannot be executed normally. There is this phenomenon in the early version of RealPlayer, and I don't know if it is used. 5) MUTEX / EVENT / SEMAPHORE determines if the instance is existing by a thread synchronization object such as a mute object / semaphore / event, and pays attention to the permissions issue (SID) under NT. 6) DLL Global Sharing Region VC DLL project can create a process shared data segment by the following code: #pragma data_seg (". Share") // Shared for All Processes That Attach to the dll dword DLLGS_DWRUNCOUNT = 1; / / Be sure to initialize the variable here, otherwise the work is white! #pragma data_seg () #pragma Comment (Linker, "/ Section: .Share, RWS") exports 3 functions, respectively: DWORD INCRUNCOUNT (VOID); // Run counter plus 1, return counter Result DWord Decruncount (void) ; // Run counter minus 1, return counter results DWORD getRuncount (Void); // Take the current Run counter because the DLL global shared section is only initialized once when it is mapped to each process address space, and is loaded when it is loaded by Windows Therefore, the program instance can be reliably counted by using the shared segment data. 7) Memory mapping (file mapping) can also control the number of program instances to operate by placing program instance information (such as window handle, counter, etc.), which can also control the number of program instances, and the truth is similar to the DLL global sharing area.

转载请注明原文地址:https://www.9cbs.com/read-80973.html

New Post(0)