Thread.IsBackground 属性
获取或设置一个值,该值指示某个线程是否为后台线程。
**命名空间:**System.Threading
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Property IsBackground As Boolean
用法
Dim instance As Thread
Dim value As Boolean
value = instance.IsBackground
instance.IsBackground = value
public bool IsBackground { get; set; }
public:
property bool IsBackground {
bool get ();
void set (bool value);
}
/** @property */
public boolean get_IsBackground ()
/** @property */
public void set_IsBackground (boolean value)
public function get IsBackground () : boolean
public function set IsBackground (value : boolean)
属性值
如果此线程是后台线程或即将成为后台线程,则为 true;否则为 false。
异常
异常类型 | 条件 |
---|---|
线程已死。 |
备注
一个线程或者是后台线程或者是前台线程。后台线程与前台线程类似,区别是后台线程不会防止进程终止。属于某个进程的所有前台线程都终止后,公共语言运行库就会结束该进程。所有剩余的后台线程都会停止且不会完成。
示例
下面的代码示例对比了前台线程与后台线程的行为。创建一个前台线程和一个后台线程。前台线程使进程保持运行,直到它完成它的 while 循环。前台线程完成后,进程在后台线程完成它的 while 循环之前终止。
Imports System
Imports System.Threading
Public Class Test
<MTAThread> _
Shared Sub Main()
Dim shortTest As New BackgroundTest(10)
Dim foregroundThread As New Thread(AddressOf shortTest.RunLoop)
foregroundThread.Name = "ForegroundThread"
Dim longTest As New BackgroundTest(50)
Dim backgroundThread As New Thread(AddressOf longTest.RunLoop)
backgroundThread.Name = "BackgroundThread"
backgroundThread.IsBackground = True
foregroundThread.Start()
backgroundThread.Start()
End Sub
End Class
Public Class BackgroundTest
Dim maxIterations As Integer
Sub New(maximumIterations As Integer)
maxIterations = maximumIterations
End Sub
Sub RunLoop()
Dim threadName As String = Thread.CurrentThread.Name
For i As Integer = 0 To maxIterations
Console.WriteLine("{0} count: {1}", _
threadName, i.ToString())
Thread.Sleep(250)
Next i
Console.WriteLine("{0} finished counting.", threadName)
End Sub
End Class
using System;
using System.Threading;
class Test
{
static void Main()
{
BackgroundTest shortTest = new BackgroundTest(10);
Thread foregroundThread =
new Thread(new ThreadStart(shortTest.RunLoop));
foregroundThread.Name = "ForegroundThread";
BackgroundTest longTest = new BackgroundTest(50);
Thread backgroundThread =
new Thread(new ThreadStart(longTest.RunLoop));
backgroundThread.Name = "BackgroundThread";
backgroundThread.IsBackground = true;
foregroundThread.Start();
backgroundThread.Start();
}
}
class BackgroundTest
{
int maxIterations;
public BackgroundTest(int maxIterations)
{
this.maxIterations = maxIterations;
}
public void RunLoop()
{
String threadName = Thread.CurrentThread.Name;
for(int i = 0; i < maxIterations; i++)
{
Console.WriteLine("{0} count: {1}",
threadName, i.ToString());
Thread.Sleep(250);
}
Console.WriteLine("{0} finished counting.", threadName);
}
}
using namespace System;
using namespace System::Threading;
ref class BackgroundTest
{
private:
int maxIterations;
public:
BackgroundTest( int maxIterations )
{
this->maxIterations = maxIterations;
}
void RunLoop()
{
String^ threadName = Thread::CurrentThread->Name;
for ( int i = 0; i < maxIterations; i++ )
{
Console::WriteLine( "{0} count: {1}", threadName, i.ToString() );
Thread::Sleep( 250 );
}
Console::WriteLine( "{0} finished counting.", threadName );
}
};
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();
}
import System.*;
import System.Threading.*;
import System.Threading.Thread;
class Test
{
public static void main(String[] args)
{
BackgroundTest shortTest = new BackgroundTest(10);
Thread foregroundThread = new Thread(new ThreadStart(shortTest.RunLoop));
foregroundThread.set_Name("ForegroundThread");
BackgroundTest longTest = new BackgroundTest(50);
Thread backgroundThread = new Thread(new ThreadStart(longTest.RunLoop));
backgroundThread.set_Name("BackgroundThread");
backgroundThread.set_IsBackground(true);
foregroundThread.Start();
backgroundThread.Start();
} //main
} //Test
class BackgroundTest
{
private int maxIterations;
public BackgroundTest(int maxIterations)
{
this.maxIterations = maxIterations;
} //BackgroundTest
public void RunLoop()
{
String threadName = Thread.get_CurrentThread().get_Name();
for (int i = 0; i < maxIterations; i++) {
Console.WriteLine("{0} count: {1}", threadName, String.valueOf(i));
Thread.Sleep(250);
}
Console.WriteLine("{0} finished counting.", threadName);
} //RunLoop
} //BackgroundTest
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0
请参见
参考
Thread 类
Thread 成员
System.Threading 命名空间