We often want to open a program without opening another instance. After exploration, we found that there are two solutions available: One is through the process, and the other is through the mutex class.
1. Using mutex, mutex has an output parameter that can mark whether it is a newly opened Real column. We can use this parameter for processing.
Using system;
Using system. Threading;
Public class test
{
Public static void main ()
{
// Set this variable to false if you do not want to request
// Initial ownership of the named mutex.
Bool requestinitialownership = true;
Bool mutexwascreated;
// Request initial ownership of the named mutex by passing
// True for the first parameter. Only one system object named
// "Mymutex" can exist; the local mutex object represents
// This system object. If "mymutex" is created by this call,
// Then mutexwascreated contains true; otherwise, it contains
// False.
Mutex M = new mutex (requestinitialownership, "mymutex", out mutexwascreated );
// This thread owns the mutex only if it both requested
// Initial ownership and created the named mutex. Otherwise,
// It can request the named mutex by calling waitone.
If (! (Requestinitialownership & mutexwascreated ))
{
Console. writeline ("waiting for the named mutex .");
M. waitone ();
}
// Once the process has gained control of the named mutex,
// Hold onto it until the user presses enter.
Console. writeline ("this process owns the named mutex." + "press enter to release the mutex and exit .");
Console. Readline ();
// Call releasemutex to allow other threads to gain control
// Of the named mutex. If you keep a reference to the local
// Mutex, you can call waitone to request CONTROL OF
// Named mutex.
M. releasemutex ();
}
}
2. Process
Find the process with the current process name in the process pool. It depends on whether it can be found for processing. In the following example, we first determine whether the application has been enabled. If the application is enabled, the enabled settings are maximized and the new program is not running. (The following is the winform Program)
Using system;
Using system. diagnostics;
Using system. reflection;
Using system. runtime. interopservices;
Using system. Windows. forms;
Namespace wintest
{
/// <Summary>
/// Summary of the program.
/// </Summary>
Public class program1
{
# Main entry point of the region application.
[Stathread]
Static void main ()
{
Mainform = new mainform ();
Process instance = runninginstance ();
If (instance = NULL)
{
Application. Run (mainform );
}
Else
{
Handlerunninginstance (instance );
}
}
Private Static Process runninginstance ()
{
Process current = process. getcurrentprocess ();
Process [] processes = process. getprocessesbyname (current. processname );
// Traverse a routine with the same name running
Foreach (Process in processes)
{
// Ignore existing routines
If (process. ID! = Current. ID)
{
// Ensure that the routine runs from the EXE file
If (assembly. getexecutingassembly (). Location. Replace ("/", "\\\\") =
Current. mainmodule. filename)
{
// Return another routine instance
Return process;
}
}
}
// If no other routine exists, null is returned.
Return NULL;
}
Private const int ws_shownormal = 1; // winuser. h define
Private Static void handlerunninginstance (process instance)
{
// Ensure that the window is not minimized or maximized
Showwindowasync (instance. main1_whandle, ws_shownormal );
// Set the actual routine to foreground window
Setforegroundwindow (instance. main1_whandle );
}
[Dllimport ("user32.dll")]
Private Static extern bool showwindowasync (intptr hwnd, int cmdshow );
[Dllimport ("user32.dll")]
Private Static extern bool setforegroundwindow (intptr hwnd );
# Endregion
}
}