There are times when we need to run the software alone instead of repeating it for no reason.
Let me give you an example: Can you choose to run the music playback software repeatedly?
You have a right-click shortcut open in the registration form (REG), but if the soft
is simply meant to be repeated running instances presumably users see such a soft
The expression must be very exaggerated and amusing. But what about being serious?
Avoid multiple runs of software I give two ways to be a mutex (mutex)
The other is the API event two methods are better, you can understand
The above two methods will register a string flag in the system each time the software starts
To check if a flag exists, but software destruction means that the system will automatically
Release the flag so we don't need to go to close and CloseHandle and
I don't recommend it. To shut down the kernel object of the system who knows you will not because
Cause the program or system to be unstable for the operation no one can guarantee that, right?
Ease of use I'm feeling pretty good. very humane and not fancy technology is difficult.
Using System.Threading;
BOOL Bcreatednew; Mutex mutex = new Mutex (false, "MyApp", out bcreatednew);
All we need to do is to determine if Bcreatednew is false. If not
Create success or enter false otherwise true if we want to determine if the system is running
The same software and the system does not allow us to register the same flag then surely it will be false
, the reason is easy to say Tongqiao, right. Now let's talk about the API event approach
Using System.Runtime.InteropServices;
const int NULL = 0; const int event_all_access = 2031619; if (OpenEvent (event_all_access, False, "MyApp")) { //running } else { //not running CreateEvent (NULL, False, False, "MyApp"); }
[DllImport ("kernel32.dll", SetLastError = True)] public static extern bool OpenEvent (int dwdesiredaccess, bool binherithandle, string lpname); [DllImport ("kernel32.dll", SetLastError = True)] public static extern bool CreateEvent (int lpeventattribute, BOOL bManualReset, bool bInheritHandle, string lpname);
C # Checks whether it is already running (two methods)