Thread.IsBackground 属性

定义

获取或设置一个值,该值指示某个线程是否为后台线程。Gets or sets a value indicating whether or not a thread is a background thread.

public:
 property bool IsBackground { bool get(); void set(bool value); };
public bool IsBackground { get; set; }
member this.IsBackground : bool with get, set
Public Property IsBackground As Boolean

属性值

如果此线程为或将成为后台线程,则为 true;否则为 falsetrue if this thread is or is to become a background thread; otherwise, false.

例外

线程终止。The thread is dead.

示例

下面的示例对比了前台和后台线程的行为。The following example contrasts the behavior of foreground and background threads. 它创建前台线程和后台线程。It creates a foreground thread and a background thread. 前台线程会使进程保持运行,直到完成其 for 循环,然后终止。The foreground thread keeps the process running until completes its for loop and terminates. 但是,如示例的输出所示,由于前台线程已完成执行,因此进程在后台线程完成执行之前终止。However, as the output from the example shows, because the foreground thread has finished execution, the process is terminated before the background thread has completed execution.

using namespace System;
using namespace System::Threading;

ref class BackgroundTest
{
private:
   int maxIterations;

public:
   BackgroundTest(int maxIterations)
   {
      this->maxIterations = maxIterations;
   }

   void RunLoop()
   {
      for (int i = 0; i < maxIterations; i++ )
      {
         Console::WriteLine("{0} count: {1}", 
              Thread::CurrentThread->IsBackground ? 
              "Background Thread" : "Foreground Thread", i);
         Thread::Sleep(250);

      }
      Console::WriteLine("{0} finished counting.", 
                         Thread::CurrentThread->IsBackground ? 
                         "Background Thread" : "Foreground Thread");
   }
};

int main()
{
   BackgroundTest^ shortTest = gcnew BackgroundTest( 10 );
   Thread^ foregroundThread = gcnew Thread( gcnew ThreadStart( shortTest, &BackgroundTest::RunLoop ) );
   foregroundThread->Name =  "ForegroundThread";
   BackgroundTest^ longTest = gcnew BackgroundTest( 50 );
   Thread^ backgroundThread = gcnew Thread( gcnew ThreadStart( longTest, &BackgroundTest::RunLoop ) );
   backgroundThread->Name =  "BackgroundThread";
   backgroundThread->IsBackground = true;
   foregroundThread->Start();
   backgroundThread->Start();
}

using System;
using System.Threading;

class Example
{
    static void Main()
    {
        BackgroundTest shortTest = new BackgroundTest(10);
        Thread foregroundThread = 
            new Thread(new ThreadStart(shortTest.RunLoop));

        BackgroundTest longTest = new BackgroundTest(50);
        Thread backgroundThread = 
            new Thread(new ThreadStart(longTest.RunLoop));
        backgroundThread.IsBackground = true;

        foregroundThread.Start();
        backgroundThread.Start();
    }
}

class BackgroundTest
{
    int maxIterations;

    public BackgroundTest(int maxIterations)
    {
        this.maxIterations = maxIterations;
    }

    public void RunLoop()
    {
        for (int i = 0; i < maxIterations; i++) {
            Console.WriteLine("{0} count: {1}", 
                Thread.CurrentThread.IsBackground ? 
                   "Background Thread" : "Foreground Thread", i);
            Thread.Sleep(250);
        }
        Console.WriteLine("{0} finished counting.", 
                          Thread.CurrentThread.IsBackground ? 
                          "Background Thread" : "Foreground Thread");
    }
}
// The example displays output like the following:
//    Foreground Thread count: 0
//    Background Thread count: 0
//    Background Thread count: 1
//    Foreground Thread count: 1
//    Foreground Thread count: 2
//    Background Thread count: 2
//    Foreground Thread count: 3
//    Background Thread count: 3
//    Background Thread count: 4
//    Foreground Thread count: 4
//    Foreground Thread count: 5
//    Background Thread count: 5
//    Foreground Thread count: 6
//    Background Thread count: 6
//    Background Thread count: 7
//    Foreground Thread count: 7
//    Background Thread count: 8
//    Foreground Thread count: 8
//    Foreground Thread count: 9
//    Background Thread count: 9
//    Background Thread count: 10
//    Foreground Thread count: 10
//    Background Thread count: 11
//    Foreground Thread finished counting.
Imports System.Threading

Public Module Example
    Public Sub Main()
        Dim shortTest As New BackgroundTest(10)
        Dim foregroundThread As New Thread(AddressOf shortTest.RunLoop)

        Dim longTest As New BackgroundTest(50)
        Dim backgroundThread As New Thread(AddressOf longTest.RunLoop)
        backgroundThread.IsBackground = True

        foregroundThread.Start()
        backgroundThread.Start()
    End Sub
End Module

Public Class BackgroundTest
    Dim maxIterations As Integer 

    Sub New(maximumIterations As Integer)
        maxIterations = maximumIterations
    End Sub

    Sub RunLoop()
        For i As Integer = 0 To maxIterations
            Console.WriteLine("{0} count: {1}", _
                    If(Thread.CurrentThread.IsBackground, 
                       "Background Thread", "Foreground Thread"), i)
            Thread.Sleep(250)
        Next 

        Console.WriteLine("{0} finished counting.", 
                          If(Thread.CurrentThread.IsBackground, 
                          "Background Thread", "Foreground Thread"))
    End Sub
End Class
' The example displays output like the following:
'    Foreground Thread count: 0
'    Background Thread count: 0
'    Background Thread count: 1
'    Foreground Thread count: 1
'    Foreground Thread count: 2
'    Background Thread count: 2
'    Foreground Thread count: 3
'    Background Thread count: 3
'    Background Thread count: 4
'    Foreground Thread count: 4
'    Foreground Thread count: 5
'    Background Thread count: 5
'    Foreground Thread count: 6
'    Background Thread count: 6
'    Background Thread count: 7
'    Foreground Thread count: 7
'    Background Thread count: 8
'    Foreground Thread count: 8
'    Foreground Thread count: 9
'    Background Thread count: 9
'    Background Thread count: 10
'    Foreground Thread count: 10
'    Background Thread count: 11
'    Foreground Thread finished counting.

注解

线程是后台线程或前台线程。A thread is either a background thread or a foreground thread. 后台线程与前台线程相同,不同之处在于后台线程不会阻止进程终止。Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. 属于某个进程的所有前台线程终止后,公共语言运行时将结束该进程。Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. 剩余的后台线程将停止,并且不会完成。Any remaining background threads are stopped and do not complete.

默认情况下,以下线程在前台执行(即,其 IsBackground 属性返回 false):By default, the following threads execute in the foreground (that is, their IsBackground property returns false):

  • 主线程(或主应用程序线程)。The primary thread (or main application thread).

  • 通过调用 Thread 类构造函数创建的所有线程。All threads created by calling a Thread class constructor.

默认情况下,以下线程在后台执行(也就是说,它们的 IsBackground 属性返回 true):By default, the following threads execute in the background (that is, their IsBackground property returns true):

  • 线程池线程,线程池是由运行时维护的工作线程池。Thread pool threads, which are a pool of worker threads maintained by the runtime. 您可以使用 ThreadPool 类配置线程池和计划线程池线程上的工作。You can configure the thread pool and schedule work on thread pool threads by using the ThreadPool class.

    备注

    基于任务的异步操作会在线程池线程上自动执行。Task-based asynchronous operations automatically execute on thread pool threads.

  • 从非托管代码进入托管执行环境的所有线程。All threads that enter the managed execution environment from unmanaged code.

适用于

另请参阅