Application.exit () ends the program, but the thread is still in the solution.
Most of this occurs because multithreaded programming is used, or you call a DLL that uses multithreading.
We know that normally the thread will shut down after executing the specified task, but if for some loop class thread, or forget to turn off the thread, we need to force it to close manually.
Use one of the following three to try forcing the thread to close.
Copy content to Clipboard code:Application.ExitThread();//退出当前线程上的消息循环,并关闭该线程上的所有窗口。 Copy content to Clipboard code:System.Environment.Exit(0);
//终止当前进程并为基础操作系统提供指定的退出代码Copy content to Clipboard code:Process.GetCurrentProcess().Kill();//终止当前正在运行的线程
But all of these methods are forced to close, so how do we use Application.exit () and gracefully close the entire program?
At this time we have to quote Microsoft to Application.exit (), the explanation:
Copy content to Clipboard code:Application.Exit();// 方法停止在所有线程上运行的所有消息循环,并关闭应用程序的所有窗口
We looked very clearly and stopped all the message loops running on all the threads, and since it was all, why didn't it shut down? What we have to make clear here is that the new process belongs to another domain, and what you need to know is that the process is not equal to the thread, application.exit (); It is all the threads that closed the current process and cannot close the process it created. There is no relationship between the new process and this process.
So, we can understand Application.exit ();
Copy content to Clipboard code:Application.Exit();// 方法停止在当前进程上的所有线程上运行的所有消息循环,并关闭当前进程上的应用程序的所有窗口。
So, we want to use Application.exit () on the current process, to close other processes, to set up the other process as the background process of the current process (IsBackground), and when a thread is created in the main threads, Then the isbackground of the thread is set to False by default. When the main thread exits, Isbackground=false threads continue to execute until the end of the thread execution. Only Isbackground=true threads exit with the main thread exiting. When initializing a thread, put thread.isbackground=true, indicating that the thread is a background thread. The background thread will exit with the main thread exiting.
So we'll add a sentence after the process is created:
Copy content to Clipboard code:IsBackground=true;
Then it can be controlled by the current process before it can be used application.exit ();
This article from the wash white, starting in 51cto--. NET Forum, reproduced please retain this paragraph of the text description.
Http://bbs.51cto.com/thread-970057-1.html
Application.exit () ends the program, but the thread is still in the solution.