C # restricts programs to run only one instance (anti-multi-Open)

Source: Internet
Author: User

 
//method One: Only multiple processes are allowed to runusingSystem;usingSystem.Collections.Generic;usingSystem.Windows.Forms;namespaceduoyemianie{Static classProgram {/// <summary>        ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {BOOLret; System.Threading.Mutex Mutex=NewSystem.Threading.Mutex (true, Application.productname, outret); if(ret) {System.Windows.Forms.Application.EnableVisualStyles (); //These two lines implement XP visual styleSystem.Windows.Forms.Application.DoEvents ();//These two lines implement XP visual styleSystem.Windows.Forms.Application.Run (NewLambrowser ()); //Main form for your program, if the console program does not use this sentencemutex.            ReleaseMutex (); }            Else{MessageBox.Show (NULL,"There is an application that is the same as this one that is already running, please do not run multiple programs at the same time. \ n \ nthe program is about to exit. ", Application.productname, MessageBoxButtons.OK, messageboxicon.warning); //the prompt message can be deleted. Application.exit ();//Exit Program            }        }    }}

//method Two: Prevent multiple processes from running and activate the previous process when run repeatedlyusingSystem;usingSystem.Collections.Generic;usingSystem.Windows.Forms;usingSystem.Diagnostics;usingSystem.Runtime.InteropServices;usingSystem.Reflection;namespaceduoyemianie{Static classProgram {/// <summary>        ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {//Get the running instance. Process instance =runninginstance (); if(Instance = =NULL) {System.Windows.Forms.Application.EnableVisualStyles (); //These two lines implement XP visual styleSystem.Windows.Forms.Application.DoEvents (); //there isn ' t another instance, show our form. System.Windows.Forms.Application.Run (NewLambrowser ()); }        Else    {        //There is another instance of this process. Handlerunninginstance (instance); }}    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 processinchprocesses) {        //Ignore The current process        if(Process. Id! =Current . Id) {//Make sure, the process is running from the exe file.                        if(Assembly.getexecutingassembly (). Location.replace ("/","\\") ==Current . Mainmodule.filename) {//Return The other process instance.                 returnprocess; }        }    }    //No Other instance is found, return null.    return NULL;} Public Static voidHandlerunninginstance (Process instance) {//Make sure the window was not minimized or maximizedShowwindowasync (instance.    Mainwindowhandle, Ws_shownormal); //Set the real intance to foreground windowSetForegroundWindow (instance. Mainwindowhandle);} [DllImport ("User32.dll")]Private Static extern BOOLShowwindowasync (IntPtr hWnd,intcmdshow); [DllImport ("User32.dll")]Private Static extern BOOLSetForegroundWindow (IntPtr hWnd);Private Const intWs_shownormal =1; }}

C # restricts programs to run only one instance (anti-multi-Open)

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.