当一个线程,被设置为IsBackground = true的时候,它就会放手,让主线程不用等,而主线程一退出,它就会退出。
为False时,则是要求主线程等待其执行完毕,它先退出,主线程再退出。
参考官方教程。
下面验证一下:
两个线程均为后台线程
注意看Main()
中的IsBackground
设置语句。
线程1-2运行的任务相同,但是运行次数不一样。
线程2运行时间更长一些。
本次测试中,两处均未被注释。
class Example
{static void Main(){Thread t1 = new Thread(new ThreadStart((new BackgroundTest(10)).RunLoop));t1.IsBackground = true; // 注意此处:1Thread t2 = new Thread(new ThreadStart((new BackgroundTest(50)).RunLoop));t2.IsBackground = true; // 注意此处:2t1.Start();t2.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(200);}Console.WriteLine("{0} finished counting.",Thread.CurrentThread.IsBackground ?"Background Thread" : "Foreground Thread");}
}
线程结束情况:
程序“[29084] Demo.exe”已退出,返回值为 0 (0x0)。
此代码运行时,控制台一闪而过,无任何输出。也就是两个线程均未运行起来就随Main线程退出了。
线程12均为前台线程
代码方面,将1、2两处均注释掉。
控制台输出:
Foreground Thread count: 0
Foreground Thread count: 0
Foreground Thread count: 1
Foreground Thread count: 1
Foreground Thread count: 2
Foreground Thread count: 2
Foreground Thread count: 3
Foreground Thread count: 3
Foreground Thread count: 4
Foreground Thread count: 4
Foreground Thread count: 5
Foreground Thread count: 5
Foreground Thread count: 6
Foreground Thread count: 6
Foreground Thread count: 7
Foreground Thread count: 7
Foreground Thread count: 8
Foreground Thread count: 8
Foreground Thread count: 9
Foreground Thread count: 9
Foreground Thread count: 10
Foreground Thread finished counting.
Foreground Thread count: 11
Foreground Thread count: 12
Foreground Thread count: 13
Foreground Thread count: 14
Foreground Thread count: 15
Foreground Thread count: 16
Foreground Thread count: 17
Foreground Thread count: 18
Foreground Thread count: 19
Foreground Thread count: 20
Foreground Thread count: 21
Foreground Thread count: 22
Foreground Thread count: 23
Foreground Thread count: 24
Foreground Thread count: 25
Foreground Thread count: 26
Foreground Thread count: 27
Foreground Thread count: 28
Foreground Thread count: 29
Foreground Thread count: 30
Foreground Thread count: 31
Foreground Thread count: 32
Foreground Thread count: 33
Foreground Thread count: 34
Foreground Thread count: 35
Foreground Thread count: 36
Foreground Thread count: 37
Foreground Thread count: 38
Foreground Thread count: 39
Foreground Thread count: 40
Foreground Thread count: 41
Foreground Thread count: 42
Foreground Thread count: 43
Foreground Thread count: 44
Foreground Thread count: 45
Foreground Thread count: 46
Foreground Thread count: 47
Foreground Thread count: 48
Foreground Thread count: 49
Foreground Thread finished counting.
线程结束情况:
线程 0x761c 已退出,返回值为 0 (0x0)。
线程 0x2360 已退出,返回值为 0 (0x0)。
程序“[14928] Demo.exe”已退出,返回值为 0 (0x0)。
说明Main线程会等待前台线程,直到它们结束退出,Main线程再推出。
线程1为前台进程
代码方面,只需要将1处t1.IsBackground = true;
注释掉就可以了。
控制台输出:
Foreground Thread count: 0
Background Thread count: 0
Foreground Thread count: 1
Background Thread count: 1
Foreground Thread count: 2
Background Thread count: 2
Background Thread count: 3
Foreground Thread count: 3
Background Thread count: 4
Foreground Thread count: 4
Foreground Thread count: 5
Background Thread count: 5
Background Thread count: 6
Foreground Thread count: 6
Background Thread count: 7
Foreground Thread count: 7
Background Thread count: 8
Foreground Thread count: 8
Background Thread count: 9
Foreground Thread count: 9
Foreground Thread finished counting.
Background Thread count: 10
线程结束情况:
线程 0xccc 已退出,返回值为 0 (0x0)。
程序“[33772] Demo.exe”已退出,返回值为 0 (0x0)。
上述为全部输出内容。这说明:
前台线程会执行直到结束,然后,主线程退出,最后,当所有的前台线程一结束,后台线程也立即结束。
线程2为前台进程
代码方面,只需要将2处t2.IsBackground = true;
注释掉就可以了。
控制台输出:
Foreground Thread count: 0
Background Thread count: 0
Foreground Thread count: 1
Background Thread count: 1
Background Thread count: 2
Foreground Thread count: 2
Foreground Thread count: 3
Background Thread count: 3
Background Thread count: 4
Foreground Thread count: 4
Background Thread count: 5
Foreground Thread count: 5
Foreground Thread count: 6
Background Thread count: 6
Background Thread count: 7
Foreground Thread count: 7
Foreground Thread count: 8
Background Thread count: 8
Foreground Thread count: 9
Background Thread count: 9
Background Thread finished counting.
Foreground Thread count: 10
Foreground Thread count: 11
Foreground Thread count: 12
Foreground Thread count: 13
Foreground Thread count: 14
Foreground Thread count: 15
Foreground Thread count: 16
Foreground Thread count: 17
Foreground Thread count: 18
Foreground Thread count: 19
Foreground Thread count: 20
Foreground Thread count: 21
Foreground Thread count: 22
Foreground Thread count: 23
Foreground Thread count: 24
Foreground Thread count: 25
Foreground Thread count: 26
Foreground Thread count: 27
Foreground Thread count: 28
Foreground Thread count: 29
Foreground Thread count: 30
Foreground Thread count: 31
Foreground Thread count: 32
Foreground Thread count: 33
Foreground Thread count: 34
Foreground Thread count: 35
Foreground Thread count: 36
Foreground Thread count: 37
Foreground Thread count: 38
Foreground Thread count: 39
Foreground Thread count: 40
Foreground Thread count: 41
Foreground Thread count: 42
Foreground Thread count: 43
Foreground Thread count: 44
Foreground Thread count: 45
Foreground Thread count: 46
Foreground Thread count: 47
Foreground Thread count: 48
Foreground Thread count: 49
Foreground Thread finished counting.
线程结束情况:
线程 0x6bb0 已退出,返回值为 0 (0x0)。
线程 0x3774 已退出,返回值为 0 (0x0)。
程序“[11692] Demo.exe”已退出,返回值为 0 (0x0)。
前台线程会执行直到结束,而由于前台线程执行时间长,后台线程执行时间短,故而后台线程也能执行完毕。等前台线程执行结束后,Main线程也结束。