Make sure the program has only one instance running (C #)

zhaozj2021-02-16  85

Collection of a In some scenarios, you may wish to ensure that a user can run only one instance of your application at a time. Besides ensuring that only a single instance of your application is running, you may also want to bring the instance already running To The Front And Restore IT, IF IT IS minimized.

First, to ensure that only one instance of your application is running at a time, the best method I've found is to create a mutex that is held by the operating system (thanks to Michael Covington). This will put a request to the operating system that a mutex be created if one does not already exist. Only one mutex can ever be created at a time, so if you request a new one and it can not be created, you can safely assume that your application is already running.

Using system.threading job.runtime.interopservices;

Public Class Form1: form {[stathread] static void main () {bool creatednew;

Mutex M = New Mutex (True, "Yourappname", Out CreatedNew);

IF (! creatednew) {// app is already Running ... MessageBox.show ("Only One Instance of this Application IS Allowed At a Time."); Return;}

Application.run (New Form1 ());

// Keep The Mutex Reference Alive Until The Normal Termination of The Program Gc.keepalive (m);}}

The above code will work for the vast majority of your needs. It will also run under scenarios where your code is executing with less than FullTrust permissions (see Code Access Security in MSDN for further information).

If your application can run with Full Trust permissions, we can take this a step further and find the window of the application instnace already running and bring it to the front for the user: public class Form1: Form {[STAThread] static void Main ( ) {Bool creatednew;

System.threading.mutex m = new system.threading.mutex (True, "YOURAPPNAME", OUT CREATEDNEW);

IF (! creatednew) {// see if we can find The other app and bring it to front intptr hwnd = findwindow ("WindowsForms10.Window.8.app3", "YOURAPPNAME");

IF (hwnd! = INTPTR.ZERO) {form1.windowplacement placement = new form1.windowplace (); placement.length = marshal.sizeof (Placement);

GetWindowPlacement (HWND, Ref Placement);

IF (placement.showcmd! = sw_normal) {placement.showcmd = sw_restore;

Setwindowplacement (hwnd, refplayment); setForegroundWindow (hwnd);}}

Return;}

Application.run (New Form1 ());

// Keep The Mutex Reference Alive Until The Normal Termination of The Program Gc.keepalive (M);

Private const Int sw sw sw sw sw sw swhed = 9;

[DLLIMPORT ("User32", entrypoint = "findwindow")] static extern INTPTR FINDWINDOW (String ClassName, String Windown);

[DLLIMPORT ("User32", entrypoint = "sendMessage")] Private static extern INTPTR SendMessage (INTPTR HWND, UINT MSG, INTPTR WPARAM, INTPTR LPARAM);

[DllImport ( "User32", EntryPoint = "SetForegroundWindow")] private static extern bool SetForegroundWindow (IntPtr hWnd); [DllImport ( "User32", EntryPoint = "SetWindowPlacement")] private static extern bool SetWindowPlacement (IntPtr hWnd, [In] Ref windowplacement lpwndpl);

[DLLIMPORT ("User32", entrypoint = "getwindowplace")] Private static extern bool getWindowplacement (INTPTR HWND, [IN] Ref windowplacement lpwndpl);

PRIVATE STRUCT POINTAPI {Public INT X; Public Int Y;

Private struct rest; public int → public int right; public int

PRIVATE STRUCT WINDOWPLACEMENT {public Int Length; public int showcmd; public pointapi ptmAXPosition; public correct rcnormalposition;}}

As you can see, with minimal effort, you can easily add a polished touch to your application. This might even help you avoid some extra legwork in ensuring that there are no issues with running multiple instances of your app at the same time that you might Have to address.

For more information about the Platform Invoke mechanisms to call Win32 API functions, I recommend that you check out .NET Framework Solutions: In Search of the Lost Win32 API by John Mueller and Charles Petzold's seminal classic Programming Windows.

Until Longhorn comes out and more of the Windows platform becomes managed, platform invokes and interop will remain a key technology to understand and use to your advantage to fill the gaps left by the Windows Forms framework.

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

New Post(0)