The winform application prohibits multiple processes from running.

Source: Internet
Author: User

Method 1: prohibit multiple processes from running

  
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. Windows. forms; Namespace Start a new process { Static Class Program { /// <Summary> /// ApplicationProgram. /// </Summary> [Stathread] Static Void Main (){ Bool Flag; system. Threading. mutex = New System. Threading. mutex ( True , Application. productname, Out Flag ); If (FLAG ){ // Enable the visual style of the application Application. enablevisualstyles (); application. setcompatibletextrenderingdefault ( False );// Process all Windows messages in the current Message Queue Application. doevents (); application. Run ( New Form1 ()); // Release system. Threading. mutex once Mutex. releasemutex ();} Else {MessageBox. Show ( Null ," The same program is already running. Please do not run multiple programs at the same time! \ N this program is about to exit! ", Application. productname, messageboxbuttons. OK, messageboxicon. Warning); application. Exit ();}}}}

 

Method 2: prohibit multiple processes from running and activate the previous processes when they are repeated.

 
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. Windows. forms; Using System. diagnostics; Using System. reflection; Using System. runtime. interopservices; Namespace Start a new process { Static Class Program {/// <Summary> /// Main entry point of the application. /// </Summary> [Stathread] Static Void Main () {system. Diagnostics. Process instance = runninginstance (); If (Instance = Null ) {Application. enablevisualstyles (); application. setcompatibletextrenderingdefault ( False ); Application. Run ( New Form1 ());} Else {Handlerunninginstance (Instance );}} /// <Summary> /// Obtain the currently running process instance /// </Summary> /// <Returns> </returns> Public Static Process runninginstance (){ // Obtain the active process Process current = process. getcurrentprocess (); // Obtain all processes with the specified process name on the current local computer Process [] processes = process. getprocessesbyname (current. processname ); Foreach (ProcessIn Processes ){ // Ignore the current process If (Process. ID! = Current. ID ){ If (Assembly. getexecutingassembly (). Location. Replace (" / "," \\ ") = Current. mainmodule. filename ){ Return Process ;}}} // If no other process with the same name exists, null is returned. Return Null ;}// Indicates that this attribute method is made public by an unmanaged dynamic link library (DLL) as a static entry point. [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; /// <Summary> /// If another process with the same name starts, the previous instance is called. /// </Summary> /// <Param name = "instance"> </param> Private Static Void Handlerunninginstance (process instance ){ // Ensure that the form is not minimized or maximized Showwindowasync (instance. main1_whandle, ws_shownormal ); // Get the previously started process instance to the front-end window Setforegroundwindow (instance. main1_whandle );}}}

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.