Ensure that only one instance of the C # app is running

zhaozj2021-02-12  143

Ensure that only one instance of the C # app is running

[Stathread] static void main () {process instance = runninginstance (); if (instance == null) {Application.run (new mainfrm ());} else {//messagebox.show ("This program is already started!" HandlerunningInstance (Instance);}}

[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;

Private statics () {process current = process.getCurrentProcess (); process [] processes = process.getprocessesbyname (current.ProcessName);

Foreach (Process Procity In Processes) {if (Process.id! = CURRENT.ID) {if (assembly.getexecutingassembly (). location.replace ("/", "//") ==current.mainmodule.FileName) { Return process;}}}

Return NULL;

Private static void handlerunninginstance (process instance) {showwindowasync (instance.mainwindowhandle, ws_shownormal);

SetForegroundWindow (Instance.MainWindowHandle);

转载请注明原文地址:https://www.9cbs.com/read-6605.html

New Post(0)