WinForm application prevents multiple processes from running

Source: Internet
Author: User

Method One: Prohibit multiple processes from running

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Windows.Forms;namespaceOpen a new process {Static classProgram {/// <summary>        ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {BOOLFlag; System.Threading.Mutex Mutex=NewSystem.Threading.Mutex (true, Application.productname, outflag); if(flag) {//enable visual styles for an applicationApplication.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); //Process all Windows messages currently in Message Queuingapplication.doevents (); Application.Run (NewForm1 ()); //release System.Threading.Mutex one timemutex.            ReleaseMutex (); }            Else{MessageBox.Show (NULL,"The same program is already running, please do not run multiple programs at the same time!\n\n this program is about to quit!", Application.productname, MessageBoxButtons.OK, messageboxicon.warning);            Application.exit (); }        }    }}

Method Two: Prevent multiple processes from running and activate the previous process when run repeatedly

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Windows.Forms;usingSystem.Diagnostics;usingSystem.Reflection;usingSystem.Runtime.InteropServices;namespaceOpen a new process {Static classProgram {/// <summary>        ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {System.Diagnostics.Process instance=runninginstance (); if(Instance = =NULL) {application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); Application.Run (NewForm1 ()); }            Else{handlerunninginstance (instance); }        }        /// <summary>        ///Gets the process instance that is currently running/// </summary>        /// <returns></returns>         Public StaticProcess runninginstance () {//gets the currently active processProcess current =process.getcurrentprocess (); //gets all processes for the specified process name on the current local computerProcess[] Processes =Process.getprocessesbyname (current.            ProcessName); foreach(Process processinchprocesses) {                //Ignore Current process                  if(Process. Id! =Current . Id) {if(Assembly.getexecutingassembly (). Location.replace ("/","\\") ==Current . Mainmodule.filename) {returnprocess; }                }            }            //returns NULL if no other process with the same name exists            return NULL; }        //indicates that the attributed method is exposed as a static entry point by an unmanaged dynamic-link library (DLL)[DllImport ("User32.dll")]        Private Static extern BOOLShowwindowasync (IntPtr hWnd,intcmdshow); [DllImport ("User32.dll")]        Private Static extern BOOLSetForegroundWindow (IntPtr hWnd); Private Const intWs_shownormal =1; /// <summary>        ///if another process with the same name is started, the previous instance is called/// </summary>        /// <param name= "instance" ></param>        Private Static voidHandlerunninginstance (Process instance) {//ensure that the form is not minimized or maximizedShowwindowasync (instance.            Mainwindowhandle, Ws_shownormal); //Get A previously started process instance to the foreground windowSetForegroundWindow (instance.        Mainwindowhandle); }    }}

WinForm application prevents multiple processes from running

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.