Home > Backend Development > C#.Net Tutorial > How to distinguish between c# foreground and background threads

How to distinguish between c# foreground and background threads

coldplay.xixi
Release: 2020-07-09 17:41:04
forward
3517 people have browsed it

How to distinguish between c# foreground and background threads

Net’s Common Language Runtime (CLR) can distinguish between two different types of threads: foreground threads and background threads. 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 finished running. All background threads will exit when the application exits. Automatically ends.

You may not know the difference:

<strong>The difference between the foreground thread</strong> and background thread Contact:

1. Background threads will not prevent the termination of the process. A process is terminated when all foreground threads belonging to it have terminated. All remaining background threads are stopped and will not complete.

2. You can change the foreground thread to a background thread at any time by setting the Thread.IsBackground property.

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.

.net Environment The thread created using Thread is the foreground thread by default, that is, the thread attribute IsBackground=false. In the process, as long as one foreground thread has not exited, the process will not termination. The main thread is a foreground thread. The background thread regardless of whether the thread ends, as long as all foreground threads exit (including normal exit and abnormal exit), the process will automatically terminate. Generally, background threads are used to process short-term tasks. For example, in a Web server, background threads can be used to process request information sent by the client. The foreground thread is generally used to handle tasks that require long-term waiting, such as programs that monitor client requests in a Web server, or programs that regularly scan certain system resources.

Case

static void Main(string[] args)
    {
      SampleTread thead = new SampleTread(10);

      SampleTread theadback = new SampleTread(10);
      var theadone = new Thread(thead.CountNumbers);
      var theadtwo = new Thread(theadback.CountNumbers);
      theadtwo.IsBackground = true;
      theadone.Start();
      theadtwo.Start();
      
    }
Copy after login
class SampleTread
  {
    private readonly int _iterations;
    public SampleTread(int iterations)
    {
      this._iterations = iterations;
    }
    public void CountNumbers()
    {
      for (int i = 0; i < _iterations; i++)
      {
        Sleep(TimeSpan.FromSeconds(0.5));
        Console.WriteLine($"{ CurrentThread.Name}print{i}");
      }
    }
  }
Copy after login

Analysis

The running result is that after the current foreground thread completes execution, the background thread will also exit. In fact, there is no concept of front and rear threads in the operating system, so why is there such a thing?

What’s the point? This is originally the case. When our main thread ends, other threads should also end, because our thread object is released. If other threads are not released when the main thread ends, then the harm to other threads is really too great. Therefore, C# provides us with the concepts of foreground threads and background threads to make operating threads easier.

As for how to use it, it depends on the specific project. Background threads often play an auxiliary function. For example, in WinForm, the thread process is still not closed after closing the window. This is because the main thread is shut down after closing close. The main thread will be closed safely after other threads are closed.

This is why you can still see other threads. Of course, the reason why you see other threads may be that there are multiple processes, and the main threads of other processes are not closed. This requires detailed analysis.

Related learning recommendations: C video tutorial

The above is the detailed content of How to distinguish between c# foreground and background threads. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template