Ensure that only one instance of the application is running

Source: Internet
Author: User
[C #]
Public static Process RunningInstance ()
{
Process current = Process. GetCurrentProcess ();
Process [] processes = Process. GetProcessesByName (current. ProcessName );
// Loop through the running processes in with the same name
Foreach (Process 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;
}
[VB. NET]
Public Shared Function RunningInstance () As Process
Dim current As Process = Process. GetCurrentProcess ()
Dim processes As Process () = Process. GetProcessesByName (current. ProcessName)

'Loop through the running processes in with the same name
Dim process As Process
For Each process In processes
'Ignore the current process
If process. Id <> current. Id Then
'Make sure that the process is running from the exe file.
If [Assembly]. GetExecutingAssembly (). Location. Replace ("/", "/") = current. MainModule. FileName Then
'Return the other process instance.
Return process
End If
End If
Next process
'No other instance was found, return null.
Return Nothing
End Function 'runninginstance

If an existing form instance is detected, the instance is displayed for reference:


[STAThread]
Static void Main ()
{
// Get the running instance.
Process instance = RunningInstance ();
If (instance = null)
{
// There isn' t another instance, show our form.
Application. Run (new Form2 ());
}
Else
{
// There is another instance of this process.
HandleRunningInstance (instance );
}
}
Public static Process RunningInstance ()
{
Process current = Process. GetCurrentProcess ();
Process [] processes = Process. GetProcessesByName (current. ProcessName );
// Loop through the running processes in with the same name
Foreach (Process 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 );
}
[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

Source: http://blog.csdn.net/acewang/archive/2004/02/05/17627.aspx

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.