共用方式為


Thread.IsBackground 屬性

定義

取得或設定值,指出執行緒是不是背景執行緒。

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

屬性值

Boolean

如果這個執行緒是背景執行緒或者會成為背景執行緒,則為 true,否則為 false

例外狀況

執行緒已無作用

範例

下列範例會對比前景和背景執行緒的行為。 它會建立前景執行緒和背景執行緒。 前景執行緒會讓進程繼續執行,直到完成其 for 迴圈並結束為止。 不過,如範例的輸出所示,因為前景執行緒已完成執行,所以進程會在背景執行緒完成執行之前終止。

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.

備註

執行緒可以是背景執行緒或前景執行緒。 背景執行緒與前景執行緒相同,不同之處在于背景執行緒不會防止進程終止。 一旦所有屬於進程的前景執行緒終止之後,common language runtime 就會結束進程。 任何剩餘的背景執行緒都會停止,而且不會完成。

根據預設,下列執行緒會在前景中執行 (也就是其 IsBackground 屬性會傳回 false) :

  • 主要執行緒 (或主應用程式執行緒) 。

  • 藉由呼叫類別的函式來建立的所有線程 Thread

根據預設,下列執行緒會在背景中執行 (也就是其 IsBackground 屬性會傳回 true) :

  • 執行緒集區執行緒,這是由執行時間維護的背景工作執行緒集區。 您可以使用類別,線上程集區執行緒上設定執行緒集區和排程工作 ThreadPool

    注意

    以工作為基礎的非同步作業會自動線上程集區執行緒上執行。

  • 從非受控碼進入 managed 執行環境的所有線程。

適用於

另請參閱