While the program is running, you cannot have multiple instances running, and you need the program to restart (rerun), so the code if the following code:
Static voidMain () {BOOLCreateNew; using(System.Threading.Mutex m =NewSystem.Threading.Mutex (true, Application.productname, outCreateNew)) { if(CreateNew) {application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); Application.Run (NewForm1 ()); } Else{MessageBox.Show ("Only one instance of this application is allowed!"); } } }
Boolean creatednew; Returns whether the initial ownership of the mutex using the thread is given
System.Threading.Mutex instance = new System.Threading.Mutex (True, "Mutexname", out creatednew); Synchronization Primitive Variables
if (creatednew)//gives thread initial ownership, which is the first use of the mutex
{
Application.Run (New Form1 ()); /s/This sentence is automatically written by the system.
Instance. ReleaseMutex ();
}
Else
{
MessageBox.Show ("A program has been started, please exit first!") "," System hint ", messageboxbuttons.ok,messageboxicon.error);
Application.exit ();
}
The above code is implemented to prohibit the multi-boot function.
At the same time, the program shutdown reboot is implemented by the following code:
Process.Start (Process.getcurrentprocess (). ProcessName + ". exe");
Application.exit ();
Then there is a problem, the program automatically shut down when the restart will be prompted to start a program.
How should I solve it?
It's okay to start again after it's closed.
But now auto-shutdown, auto-reboot sometimes succeeds, and sometimes it's blocked by the multi-boot.
Then you have to restart it manually.
For example, click the "Restart" button to execute the following code:
Process.Start (Process.getcurrentprocess (). ProcessName + ". exe");
Application.exit ();
It then starts a new process before exiting the current program.
At this point, you will encounter the code that prohibits multi-booting in Program.cs. It's not going to start automatically.
Solution Solutions
-----------------------------
Workaround One:
General Procedures:
Because the process has not been aborted, but also accounted for in memory in order to error.
This can happen if the application is closed but the process is still in memory, using multiple threads where the thread does not end and is not placed as a background thread.
You can use Application.exitthread () and abort all threads in the process.
You can also get the ID of the process in process execution, then get the process through Process.getprocessbyid (), then kill it and start a new process.
Workaround Two:
Or when the user points [restart], the mutex is released first? You might need to make that mutex variable a global, so you can access it in two places. Then, when the program exits (the sentence below Application.Run), check that if the mutex has been released, do not release it again.
Workaround Three:
Or at the point [restart] set another different semaphore, when the second program is re-loaded if you see this signal is automatically restart the situation, will not error and straight down the normal. This semaphore can be released after the first program [reboot] has been executed, but it should also be checked when the entire program exits if it exists.
C # Implementation program starts only once (implements program self-restart)