Net's Common Language Runtime (CLR) can distinguish between two different types of threads: foreground thread and background thread. The difference between the two is that the application must run all foreground threads before it can exit; while for background threads, the application can exit directly without considering whether it has been run. All background threads will automatically end when the application exits.
I may not know the difference:
The difference and connection between foreground threads and background threads:
1. Background threads will not prevent the process from termination. After all foreground threads belonging to a process are terminated, the process will be terminated. All remaining background threads will stop and will not complete.
2. You can modify the foreground thread to a background thread at any time by setting properties.
3. Whether it is a foreground thread or a background thread, if an exception occurs in the thread, it will cause the process to terminate.
4. The threads in the managed thread pool are all background threads, and the threads created using the new Thread method are all foreground threads by default.
The threads created by the .net environment using Thread are foreground threads by default, that is, the thread property IsBackground=false. In the process, as long as there is a foreground thread that does not exit, the process will not terminate. The main thread is a foreground thread. The background thread will automatically terminate regardless of whether the thread is terminated or not, as long as all foreground threads exit (including normal exit and abnormal exit). Generally, background threads are used to handle tasks with shorter time. For example, in a web server, background threads can be used to process request information sent by the client. Foreground threads are generally used to handle tasks that require long-term waiting, such as programs that listen to client requests in a web server, or programs that regularly scan certain system resources.
refer to:https:///article/
Case
static void Main(string[] args) { SampleTread thead = new SampleTread(10); SampleTread theadback = new SampleTread(10); var theadone = new Thread(); var theadtwo = new Thread(); = true; (); (); }
class SampleTread { private readonly int _iterations; public SampleTread(int iterations) { this._iterations = iterations; } public void CountNumbers() { for (int i = 0; i < _iterations; i++) { Sleep((0.5)); ($"{ }print{i}"); } } }
analyze
The result of the operation is that after the current thread is executed, the background thread will also exit. In fact, there is no concept of front and back threads in the operating system, so why is this thing there?
What is the significance? This is how it is like this when our main thread ends, other threads should also end, because our thread object is released. If the main thread ends, the harm of other threads is really too great. Therefore, c# provides us with the concepts of foreground threads and background threads to make it easier to operate threads.
As for how to use it, it depends on the specific project. Background threads often play auxiliary functions. For example, winform, the thread process is still not closed after closing the window. This is because after closing the close, it is executed to close the main thread, and the main thread will be closed safely after other threads are closed.
This is why you can also see other threads. Of course, the reason why you see there are other threads may be that multiple processes, and the main thread of other processes is not closed, which needs to be analyzed in detail.
The above is the detailed content of the difference and connection between c# foreground threads and background threads. For more information about c# foreground threads and background threads, please pay attention to my other related articles!