C#的Thread.CurrentThread.IsBackground的作用

文章讨论了C#中线程的IsBackground属性如何影响线程的执行顺序,当设置为true时,后台线程独立运行,主线程不等待;设置为false时,主线程会等待所有前台线程结束。实验证明了这一特性并给出了相应的代码示例。
摘要由CSDN通过智能技术生成

当一个线程,被设置为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;     // 注意此处:1
        Thread t2 = new Thread(new ThreadStart((new BackgroundTest(50)).RunLoop));
        t2.IsBackground = true;     // 注意此处:2

        t1.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线程也结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值