C#多线程——前台线程和后台线程
来源:互联网 发布:如何用手机淘宝发文件 编辑:程序博客网 时间:2024/05/14 22:03
由于时间片的原因,虽然所有线程在微观上是串行执行的,但在宏观上可以认为是并行执行。
线程有两种类型:前台和后台。我们可以通过线程属性IsBackground=false来指定线程的前后台属性(默认是前台线程)。
区别是:前台线程的程序,必须等所有的前台线程运行完毕后才能退出;而后台线程的程序,只要前台的线程都终止了,那么后台的线程就会自动结束并推出程序。
用法方向:一般前台线程用于需要长时间等待的任务,比如监听客户端的请求;后台线程一般用于处理时间较短的任务,比如处理客户端发过来的请求信息。
【前台线程】
using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace Demo{ class Program { static void Main(string[] args) { Thread aThread = new Thread(threadFunction); Console.WriteLine("Thread is starting..."); aThread.Start(); Console.WriteLine("Application is terminating..."); } public static void threadFunction() { Console.WriteLine("Thread is sleeping..."); Thread.Sleep(5000); Console.WriteLine("Thread is aborted!"); } }}
【后台线程】
using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace Demo{ class Program { static void Main(string[] args) { Thread aThread = new Thread(threadFunction); aThread.IsBackground = true; Console.WriteLine("Thread is starting..."); aThread.Start(); Console.WriteLine("Application is terminating..."); } public static void threadFunction() { Console.WriteLine("Thread is sleeping..."); Thread.Sleep(5000); Console.WriteLine("Thread is aborted!"); } }}
- C#多线程——前台线程和后台线程
- C#多线程——前台线程和后台线程
- C#构建多线程应用程序(3) —— 前台线程和后台线程
- 多线程-前台和后台线程
- 【多线程-前台线程和后台线程】
- c# 后台线程和前台线程
- 线程学习3——前台线程和后台线程
- C# 后台线程,前台线程
- C# 前台线程 后台线程
- 前台线程和后台线程
- 前台线程和后台线程
- 前台线程和后台线程
- 前台线程和后台线程
- 前台线程和后台线程
- 前台线程和后台线程
- 前台线程和后台线程
- 前台线程和后台线程
- 前台线程和后台线程
- DM入门知识
- 简单验证码图片识别BMP,C,C++,C#,JAVA,脚本-网络攻防大赛笔记(1)
- 关于学习输入流,输出流的总结。
- NSString,nsmutablestring,nsset,nsdictionary
- 递归和重载
- C#多线程——前台线程和后台线程
- Delphi数据库处理组件BDE、ADO、InterBase和dbExpress详细说明 .
- linux中的文件压缩
- Android导入的项目再次启动eclipse时提示The import android cannot be resolved的解决办法
- 运行的第一个android程序
- DBExpress入门 .
- 结构之法 算法之道
- shell /bin/bash^M: bad interpreter错误解决
- 第八周实验报告1