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