C#多线程——前台线程和后台线程
2012-04-08 11:55
162 查看
由于时间片的原因,虽然所有线程在微观上是串行执行的,但在宏观上可以认为是并行执行。
线程有两种类型:前台和后台。我们可以通过线程属性IsBackground=false来指定线程的前后台属性(默认是前台线程)。
区别是:前台线程的程序,必须等所有的前台线程运行完毕后才能退出;而后台线程的程序,只要前台的线程都终止了,那么后台的线程就会自动结束并推出程序。
用法方向:一般前台线程用于需要长时间等待的任务,比如监听客户端的请求;后台线程一般用于处理时间较短的任务,比如处理客户端发过来的请求信息。
【前台线程】
【后台线程】
线程有两种类型:前台和后台。我们可以通过线程属性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#多线程中(前台线程与后台线程的区别)
- CLR via C# 读书笔记 1-3 前台线程和后台线程
- 前台线程与后台线程的选择
- java中线程的前台线程和后台线程
- 多线程开发(1):主线程,前台线程,后台线程,守护线程,子线程,托管线程的关系
- C#.Net前台线程与后台线程的区别
- java前台线程和后台线程
- 前台线程与后台线程
- 前台线程(Foreground Threads)和后台线程(Background Threads)
- SWT中从后台线程访问前台界面组件的问题
- 后台线程与前台线程在运行性能上有什么区别
- java前台线程和后台线程
- 前台线程和后台线程的区别
- 前台线程(Foreground Threads)和后台线程(Background Threads)
- setDaemon(true)后就是后台线程(守护线程 ),反之就是前台线程(用户线程)
- C#前台线程与后台线程的区别和联系 (转)
- 前台线程和后台线程
- linux中,将线程放到后台,前台,暂停的方法
- [一点一滴.NET]前台线程和后台线程