I. Introduction
Recently found many people in the Forum asked how to prevent the program from being run many times the problem, such as: http://social.msdn.microsoft.com/Forums/zh-CN/6398fb10-ecc2-4c03-ab25-d03544f5fcc9, so here is recorded, hope to have a reference for friends who encounter the same problem, it is also an accumulation of yourself. Before introducing the specific implementation code, we must clarify the ideas for solving this problem? Here is one of my ways of thinking:
From the analysis process above, we can see that our solution to this problem is to start with the process. The third point is to start with the number of processes, and the fourth point is to start with the process, it's just a transformation. Let a variable uniquely identify a process. If the variable exists, the program process also runs.
Ii. Mutex
After understanding the main implementation ideas, the following code implementation is completely not a problem. The implementation of mutex is the embodiment of the fourth idea, we create a Mutex object variable for the program process. When the program runs, the program process has the Mutex variable. If the program runs again, check whether the mutex variable exists (to replace and check whether the process exists). If yes, the program is running. Otherwise, the program is not running .. Let's take a look at the implementation code below:
Method 1: Use Mutex mutex = Mutex (, Application. ProductName, System. Environment. Exit (
3. Determine whether a process exists to solve this problem. 3.1 determine the number of processes of the program.
After analyzing the above ideas, I believe that you will see the following code at a Glance. Here we will not explain it much. Let's look at the Code directly:
Method 2: Use process name = (processcollection. Length> = Application. Run (
3.2 directly determine whether a program process exists
Method 3: Use the Win32 function declaration [DllImport (ShowWindow (IntPtr hWnd, [DllImport (WS_SHOWNORMAL = method 3: Call Win32 API, and the window that activates the running program is displayed in the frontend Process = (process = MessageBox. show (method defined in method 3: Process currentProcess = Process [] processcollection = Process. getProcessesByName (currentProcess. processName. replace (, (Process process (process. id! = (Assembly. GetExecutingAssembly (). Location. Replace (,) =
3.3 solve the problem existing in the 3.2 Implementation Method-only the minimal form can be displayed. If hidden in the tray, the running program cannot be displayed.
Method 4: Win32 function declaration [DllImport (IntPtr FindWindow (lpClassName, [DllImport (, SetLastError = SwitchToThisWindow (IntPtr hWnd, [DllImport (, EntryPoint =, charSet = ShowWindow (IntPtr hwnd, SW_RESTORE = Method 4: The hidden program in the tray shows Process currentproc = Process. getProcessesByName (currentproc. processName. replace (, (processcollection. length> = (Process process (process. id! = (Process. main1_whandle. ToInt32 () = formhwnd = FindWindow (, SwitchToThisWindow (process. main1_whandle,
Iv. Program Implementation
The Running Effect of the four implementation methods is similar. Here we use implementation method 1 as a demonstration. The specific implementation effect is as follows:
V. Summary
The main reason for writing this topic is that some friends in the Forum have asked such a question and I have answered it, so I will summarize the specific implementation code to help my friends who encounter the same problem make a reference. It is also a learning accumulation and review for myself. All source code of the program is attached below:
Source code of this topic: http://files.cnblogs.com/zhili/OnlyInstanceRunning.zip