PreventProgramThere are multiple methods to run multiple instances, such as using mutex and process name. what I want to achieve is that the first instance is activated when multiple instances are running, so that the focus is obtained and displayed on the front end.
It mainly uses two API functions:
- Showwindowasync this function sets the display status of Windows generated by different threads.
- Setforegroundwindow this function sets the thread that creates the specified window to the foreground and activates the window. Enter the keyboard to switch to the window and change various visual marks for the user. The system assigns a higher permission to the thread that creates the foreground window than other threads.
CodeAs follows:
REFERENCE The following namespace:
Using system. runtime. interopservices;
Using system. diagnostics;
Using system. reflection;
//************************************** ***************
Static class Program
{
/// <Summary>
/// This function sets the display status of Windows generated by different threads.
/// </Summary>
/// <Param name = "hwnd"> window handle </param>
/// <Param name = "cmdshow"> specify how the window is displayed. To view the allowed value list, see the description section of the showwlndow function. </Param>
/// <Returns> if the function is visible, the return value is non-zero. If the function is hidden, the return value is zero. </Returns>
[Dllimport ("user32.dll")]
Private Static extern bool showwindowasync (intptr hwnd, int cmdshow );
/// <Summary>
/// This function sets the thread that creates the specified window to the foreground and activates the window. Enter the keyboard to switch to the window and change various visual marks for the user. The system assigns a higher permission to the thread that creates the foreground window than other threads.
/// </Summary>
/// <Param name = "hwnd"> the window handle is activated and transferred to the foreground. </Param>
/// <Returns> If the window is set to the foreground, the return value is non-zero. If the window is not set to the foreground, the return value is zero. </Returns>
[Dllimport ("user32.dll")]
Private Static extern bool setforegroundwindow (intptr hwnd );
Private const int ws_shownormal = 1;
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
Process instance = runninginstance ();
If (instance = NULL)
{
Form1 FRM = new form1 ();
Application. Run (New form1 ());
}
Else
{
Handlerunninginstance (instance );
}
}
/// <Summary>
/// Get the running instance. If no running instance is returned, null is returned;
/// </Summary>
Public static process runninginstance ()
{
Process current = process. getcurrentprocess ();
Process [] processes = process. getprocessesbyname (current. processname );
Foreach (Process in processes)
{
If (process. ID! = Current. ID)
{
If (assembly. getexecutingassembly (). Location. Replace ("/", "\") = current. mainmodule. filename)
{
Return process;
}
}
}
Return NULL;
}
///
/// displays the running program.
//
Public static void handlerunninginstance (process instance)
{< br> showwindowasync (instance. mainwindowhandle, ws_shownormal); // display, comment out
setforegroundwindow (instance. main1_whandle); // put it to the front end
}< BR >}