C # Only One winfrom process can be started

Source: Internet
Author: User

Method 1: prohibit multiple processes from running.
[Stathread]
Public static void main ()
{
Bool ret;
System. Threading. mutex = new system. Threading. mutex (true, application. productname, out RET );
If (RET)
{System. Windows. Forms. application. enablevisualstyles (); // these two lines implement the XP visual style.
System. Windows. Forms. application. doevents ();
System. Windows. Forms. application. Run (New Main ());
// Main is for you Program The main form of, if the console program does not use this sentence
Mutex. releasemutex ();
}
Else
{
MessageBox. Show (null, "one application with the same program is already running. Please do not run multiple programs at the same time. /N is about to exit. ", Application. productname, messageboxbuttons. OK, messageboxicon. Warning );
// Message, which can be deleted.
Application. Exit (); // exit the program
}
}

Method 2: prohibit multiple processes from running and activate the previous processes when they are repeated.
[Stathread]
Public static void main ()
{
// Get the running instance.
Process instance = runninginstance ();
If (instance = NULL)
{System. Windows. Forms. application. enablevisualstyles (); // these two lines implement the XP visual style.
System. Windows. Forms. application. doevents ();
// There isn' t another instance, show our form.
Application. Run (New Main ());
}
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 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;

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.