我是靠谱客的博主 热心夏天,这篇文章主要介绍C# Thread.Join();Thread.Abort();,现在分享给大家,希望可以做个参考。

Join() 等待当前线程运行完成后,才继续执行主线程后续代码;

Abort() 结束当前线程,继续执行主线程后续代码;

 

 

 

 Thread.Join();

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static void Main(string[] args) { Console.WriteLine("Starting program..."); Thread t = new Thread(PrintNumbersWithDelay); t.Start(); t.Join();//在继续执行标准的 COM 和 SendMessage 消息泵处理期间,阻塞调用线程,直到某个线程终止为止。 Console.WriteLine("Thread completed"); Console.Read(); } static void PrintNumbersWithDelay() { Console.WriteLine("Starting..."); for (int i = 1; i < 10; i++) { Thread.Sleep(TimeSpan.FromSeconds(2)); Console.WriteLine(i); } }

 

 

Thread.Abort();//

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
static void Main(string[] args) { Console.WriteLine("Starting program..."); Thread t = new Thread(PrintNumbersWithDelay); t.Start(); Thread.Sleep(TimeSpan.FromSeconds(6)); t.Abort();//在调用此方法的线程上引发 System.Threading.ThreadAbortException,以开始终止此线程的过程。调用此方法通常会终止线程。 Console.WriteLine("A thread has been aborted"); Console.Read(); } static void PrintNumbersWithDelay() { Console.WriteLine("Starting..."); for (int i = 1; i < 10; i++) { Thread.Sleep(TimeSpan.FromSeconds(2)); Console.WriteLine(i); } }

 

 

/

转载于:https://www.cnblogs.com/lanyubaicl/p/11179530.html

最后

以上就是热心夏天最近收集整理的关于C# Thread.Join();Thread.Abort();的全部内容,更多相关C#内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部