Thread.IsBackground プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
スレッドがバックグラウンド スレッドであるかどうかを示す値を取得または設定します。
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
。それ以外の場合は 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.
注釈
スレッドは、バックグラウンドスレッドまたはフォアグラウンドスレッドのいずれかです。 バックグラウンドスレッドは、プロセスが終了しないようにすることを除けば、フォアグラウンドスレッドと同じです。 プロセスに属するすべてのフォアグラウンドスレッドが終了すると、共通言語ランタイムによってプロセスが終了します。 残りのバックグラウンドスレッドは停止され、完了しません。
既定では、次のスレッドはフォアグラウンドで実行されます (つまり、 IsBackground プロパティはを返し false
ます)。
プライマリスレッド (またはメインアプリケーションスレッド)。
クラスコンストラクターを呼び出すことによって作成されるすべて Thread のスレッド。
既定では、次のスレッドがバックグラウンドで実行されます (つまり、 IsBackground プロパティはを返し true
ます)。
スレッドプールスレッド。ランタイムによって管理されるワーカースレッドのプールです。 スレッドプールを構成し、クラスを使用してスレッドプールスレッドの作業をスケジュールすることができ ThreadPool ます。
注意
スレッドプールのスレッドでは、タスクベースの非同期操作が自動的に実行されます。
アンマネージコードからマネージ実行環境に入るすべてのスレッド。