当前位置: 首页 > news >正文

C#的Thread.CurrentThread.IsBackground的作用

当一个线程,被设置为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线程也结束。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • STM32一个地址未对齐引起的 HardFault 异常
  • Golang | Leetcode Golang题解之第8题字符串转换整数atoi
  • 【游戏逆向】游戏全屏捡物的实现
  • 【运输层】网络数据报协议 UDP
  • 指针的深入理解(六)
  • Prime Ring Problem(UVA 524)
  • 基于Springboot+Vue实现前后端分离酒店管理系统
  • 常规的k8s的监控指标
  • 微信小程序 电影院售票选座票务系统5w7l6
  • fakebook-攻防世界
  • JVM字节码与类加载——字节码指令集与解析
  • Java | Leetcode Java题解之第13题罗马数字转整数
  • I2C协议介绍
  • Vue - 你知道Vue中computed和watch的区别吗
  • RabbitMQ系统监控、问题排查和性能优化实践
  • @jsonView过滤属性
  • [译]如何构建服务器端web组件,为何要构建?
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • docker容器内的网络抓包
  • JavaScript异步流程控制的前世今生
  • MySQL数据库运维之数据恢复
  • nodejs实现webservice问题总结
  • Promise面试题,控制异步流程
  • Spring Cloud(3) - 服务治理: Spring Cloud Eureka
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • windows下使用nginx调试简介
  • 编写符合Python风格的对象
  • 产品三维模型在线预览
  • 大快搜索数据爬虫技术实例安装教学篇
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 高程读书笔记 第六章 面向对象程序设计
  • 如何解决微信端直接跳WAP端
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 线上 python http server profile 实践
  • 要让cordova项目适配iphoneX + ios11.4,总共要几步?三步
  • 怎么将电脑中的声音录制成WAV格式
  • 正则学习笔记
  • ​浅谈 Linux 中的 core dump 分析方法
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • # Redis 入门到精通(九)-- 主从复制(1)
  • # 利刃出鞘_Tomcat 核心原理解析(二)
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • (1)SpringCloud 整合Python
  • (11)MSP430F5529 定时器B
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (附源码)c#+winform实现远程开机(广域网可用)
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (算法)N皇后问题
  • (转)JAVA中的堆栈
  • (转)setTimeout 和 setInterval 的区别
  • ... 是什么 ?... 有什么用处?
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .NET 中让 Task 支持带超时的异步等待