C # Let the application run only one instance

Source: Internet
Author: User

Use the following method to ensure the applicationProgramRun only one instance.

Add reference

Using System. reflection;
Using System. runtime. interopservices;
Using System. diagnostics;

Call DLL files [Dllimport ( " User32.dll " )]
Private   Static   Extern   Bool Showwindowasync (intptr hwnd, Int Cmdshow );
[Dllimport ( " User32.dll " )]
Private   Static   Extern   Bool Setforegroundwindow (intptr hwnd );
Private   Const   Int Ws_shownormal =   1 ;

Add the following two functions:Public StaticProcess runninginstance ()
{
Process current=Process. getcurrentprocess ();
Process [] Processes=Process. getprocessesbyname (current. processname );

// Loop through the running processes in with the same name
Foreach (Process In Processes)
{
// Ignore the current process
If (Process. ID ! = Current. ID)
{
// Make sure that the process is running from the EXE file.
If (Assembly. getexecutingassembly (). Location. Replace ( " / " , " \\ " ) =
Current. mainmodule. filename)
{
// Return the other process instance.
Return Process;
}
}
}

// No other instance was found, return null.
Return   Null ;
}
Public   Static   Void Handlerunninginstance (process instance)
{
// Make sure the window is not minimized or maximized
Showwindowasync (instance. main1_whandle, ws_shownormal );

//Set the real intance to foreground window
Setforegroundwindow (instance. main1_whandle );
}

Add the following in main:CodePublic static void main ()
{
Process instance=Runninginstance ();
If (Instance=Null)
{
Application. Run (New form1 ());
}
Else
{
Handlerunninginstance (Instance);
}
}

Add reference

Using System. reflection;
Using System. runtime. interopservices;
Using System. diagnostics;

Call DLL files [Dllimport ( " User32.dll " )]
Private   Static   Extern   Bool Showwindowasync (intptr hwnd, Int Cmdshow );
[Dllimport ( " User32.dll " )]
Private   Static   Extern   Bool Setforegroundwindow (intptr hwnd );
Private   Const   Int Ws_shownormal =   1 ;

Add the following two functions:Public StaticProcess runninginstance ()
{
Process current=Process. getcurrentprocess ();
Process [] Processes=Process. getprocessesbyname (current. processname );

// Loop through the running processes in with the same name
Foreach (Process In Processes)
{
// Ignore the current process
If (Process. ID ! = Current. ID)
{
// Make sure that the process is running from the EXE file.
If (Assembly. getexecutingassembly (). Location. Replace ( " / " , " \\ " ) =
Current. mainmodule. filename)
{
// Return the other process instance.
Return Process;
}
}
}

// No other instance was found, return null.
Return   Null ;
}
Public   Static   Void Handlerunninginstance (process instance)
{
// Make sure the window is not minimized or maximized
Showwindowasync (instance. main1_whandle, ws_shownormal );

//Set the real intance to foreground window
Setforegroundwindow (instance. main1_whandle );
}

Add the following code to main:Public static void main ()
{
Process instance=Runninginstance ();
If (Instance=Null)
{
Application. Run (New form1 ());
}
Else
{
Handlerunninginstance (Instance);
}
}

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.