You can use a mutex class to create a name-exclusive mutex, and the system can recognize a named mutex, so you can use the mutex class to prohibit the application from starting two times.
The following is the demo code for this scenario.
Using system;using system.threading;namespace run1instanceexample{ class program { [STAThread] static void Main (string[] args) { bool creatednew; var mutex = new Mutex (false, "Singletonapp", out creatednew); if (!creatednew) { return; } else { Console.WriteLine ("Good job!"); Console.ReadLine ();}}}
A mutex with 3 parameters is used in the code constructor to create a mutex object.
The 1th parameter specifies whether the mutex should initially be owned by the keynote thread;
The 2nd parameter defines the name of the mutex;
The 3rd parameter is an output parameter that receives a Boolean value that indicates whether the mutex is new. If False is returned, the mutex is already defined, and the application that created the mutex is started, the application cannot be started, or the application is started.
C # multithreaded Development 9: Use the mutex class to prevent an application from starting twice