个性化阅读
专注于IT技术分析

如何在C#中检查线程是否为后台线程?

众所周知, 线程是由Thread类创建和管理的。因此, Thread类提供了一个称为IsBackground属性检查给定线程是在后台运行还是在前台运行。如果值IsBackground设置为true, 则表示该线程是后台线程。或者如果IsBackground设置为false, 则表示该线程是前台线程,下面详细为你介绍如何检查线程是否为后台线程。

语法如下:

public bool IsBackground { get; set; }

返回值:该属性返回true如果此线程是或将要成为后台线程, 否则它将返回, false.

例外:该属性将给ThreadStateException如果线程已死。

范例1:

// C# program to illustrate the
// concept of Background thread
using System;
using System.Threading;
  
class GFG {
  
     // Main method
     static void Main( string [] args)
     {
         // Creating and initializing thread
         Thread mythr = new Thread(mythread);
  
         // Name of the thread is Geek thread
         mythr.Name = "Geek thread" ;
         mythr.Start();
  
         // IsBackground is the property
         // of Thread which allows thread
         // to run in the background
         mythr.IsBackground = true ;
  
         Console.WriteLine( "Main Thread Ends!!" );
     }
  
     // Static method
     static void mythread()
     {
  
         // Display the name of the
         // current working thread
         Console.WriteLine( "In progress thread is: {0}" , Thread.CurrentThread.Name);
  
         Thread.Sleep(1000);
  
         Console.WriteLine( "Completed thread is: {0}" , Thread.CurrentThread.Name);
     }
}

输出:

In progress thread is: Geek thread
Main Thread Ends!!

检查线程是否为后台线程示例2:

// C# program to illustrate the
// concept of IsBackground property
using System;
using System.Threading;
  
class Geeks {
  
     int J;
  
     public Geeks( int J)
     {
         this .J = J;
     }
  
     // This method will display
     // the counting of background
     // and foreground threads
     public void display()
     {
         for ( int i = 0; i < J; i++) {
              
             Console.WriteLine( "{0} count: {1}" , Thread.CurrentThread.IsBackground ? 
                            "Background Thread" : 
                         "Foreground Thread" , i);
                          
             Thread.Sleep(250);
         }
          
         Console.WriteLine( "{0} finished its counting." , Thread.CurrentThread.IsBackground ?
             "Background Thread" : "Foreground Thread" );
     }
}
  
// Driver Class
class GFG {
      
     // Main method
     static void Main()
     {
         // Creating and initializing
         // objects of Geeks class
         // Creating and initializing 
         // objects of Thread class
         Geeks obj1 = new Geeks(5);
         Thread fthr = new Thread( new ThreadStart(obj1.display));
  
         Geeks obj2 = new Geeks(10);
         Thread bthr = new Thread( new ThreadStart(obj2.display));
  
         // bthr thread is a background
         // thread because the value of
         // IsBackground property is true
         bthr.IsBackground = true ;
  
         fthr.Start();
         bthr.Start();
     }
}

输出:

Foreground Thread count: 0
Background Thread count: 0
Background Thread count: 1
Foreground Thread count: 1
Background Thread count: 2
Foreground Thread count: 2
Background Thread count: 3
Foreground Thread count: 3
Background Thread count: 4
Foreground Thread count: 4
Background Thread count: 5
Foreground Thread finished its counting.

注意:

  • 线程可以是后台线程, 也可以是前台线程。后台线程与前台线程相似, 不同之处在于后台线程不会阻止进程终止。
  • 一旦属于某个进程的所有前台线程都已终止, 公共语言运行库将终止该进程。任何剩余的后台线程都会停止并且无法完成。
  • 主线程或主应用程序线程以及Thread类构造函数创建的所有线程都在前台执行(即, 它们的IsBackground属性返回false)。
  • 线程池线程和所有从非托管代码进入托管执行环境的线程都在后台执行(即它们的IsBackground属性返回true)。

参考:

  • https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread.isbackground?view=netframework-4.7.2

赞(0) 打赏
未经允许不得转载:srcmini » 如何在C#中检查线程是否为后台线程?
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏