C#前后台线程

由于时间片的原因,虽然所有线程在微观上是串行执行的,但在宏观上可以认为是并行执行。

线程有两种类型:前台和后台。我们可以通过线程属性IsBackground=false来指定线程的前后台属性(默认是前台线程)。

区别是:前台线程的程序,必须等所有的前台线程运行完毕后才能退出;而后台线程的程序,只要前台的线程都终止了,那么后台的线程就会自动结束并推出程序。

用法方向:一般前台线程用于需要长时间等待的任务,比如监听客户端的请求;后台线程一般用于处理时间较短的任务,比如处理客户端发过来的请求信息。

【前台线程】

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Threading;  
  5.   
  6. namespace Demo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Thread aThread = new Thread(threadFunction);  
  13.             Console.WriteLine("Thread is starting...");  
  14.             aThread.Start();  
  15.             Console.WriteLine("Application is terminating...");  
  16.         }  
  17.   
  18.         public static void threadFunction()  
  19.         {  
  20.             Console.WriteLine("Thread is sleeping...");  
  21.             Thread.Sleep(5000);  
  22.             Console.WriteLine("Thread is aborted!");  
  23.         }  
  24.     }  
  25. }  


【后台线程】 

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Threading;  
  5.   
  6. namespace Demo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Thread aThread = new Thread(threadFunction);  
  13.             aThread.IsBackground = true;  
  14.             Console.WriteLine("Thread is starting...");  
  15.             aThread.Start();  
  16.             Console.WriteLine("Application is terminating...");  
  17.         }  
  18.   
  19.         public static void threadFunction()  
  20.         {  
  21.             Console.WriteLine("Thread is sleeping...");  
  22.             Thread.Sleep(5000);  
  23.             Console.WriteLine("Thread is aborted!");  
  24.         }  
  25.     }  
  26. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C#线程后台线程的举例: 1. 线程举例: ```csharp using System; using System.Threading; class Program { static void Main(string[] args) { Thread t = new Thread(new ThreadStart(CountNumbers)); t.Start(); for (int i = 1; i <= 5; i++) { Console.WriteLine("Main thread: " + i); Thread.Sleep(1000); } } static void CountNumbers() { for (int i = 1; i <= 5; i++) { Console.WriteLine("Child thread: " + i); Thread.Sleep(1000); } } } ``` 在这个例子中,我们创建了一个新的线程t,并在该线程上执行CountNumbers方法。在主线程中,我们打印了5个数字,并在每个数字之间暂停了1秒钟。在CountNumbers方法中,我们也打印了5个数字,并在每个数字之间暂停了1秒钟。由于我们在线程上执行CountNumbers方法,因此在主线程和子线程之间会交替打印数字。 2. 后台线程举例: ```csharp using System; using System.Threading; class Program { static void Main(string[] args) { Thread t = new Thread(new ThreadStart(CountNumbers)); t.IsBackground = true; t.Start(); for (int i = 1; i <= 5; i++) { Console.WriteLine("Main thread: " + i); Thread.Sleep(1000); } } static void CountNumbers() { for (int i = 1; i <= 10; i++) { Console.WriteLine("Child thread: " + i); Thread.Sleep(1000); } } } ``` 在这个例子中,我们创建了一个新的线程t,并在该线程上执行CountNumbers方法。我们将线程t设置为后台线程,这意味着当主线程退出时,线程t也会被强制终止。在主线程中,我们打印了5个数字,并在每个数字之间暂停了1秒钟。在CountNumbers方法中,我们打印了10个数字,并在每个数字之间暂停了1秒钟。由于我们在后台线程上执行CountNumbers方法,因此当主线程退出时,线程t也会被强制终止,因此只有5个数字被打印出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值