C # The program can only run once (only the same program can be opened multiple times) and activate the first instance to get the focus and display it at the frontend.

Source: Internet
Author: User

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 >}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.