dotnetCore 线程终止问题,我们用中断解决
不想看废话的直接用这个方法:Interrupt();
链接:https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.thread.interrupt?view=netcore-2.1#System_Threading_Thread_Interrupt
在今天我使用dotnetcore多线程的时候,发现一个问题。
Thread居然没有Abort() 方法。
看下图: 仅限.NET Core 不支持此成员。。。。。。,六个句号献给真香。
那么很多小伙伴还要用终止怎么办呢?
既然不不能终止,我们就中断吧:
dangdangdangdangdangdang
:
Interrupt();
使用截图如下:(执行6秒就中断的截图)
官方参考方法
https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.thread.interrupt?view=netcore-2.1#System_Threading_Thread_Interrupt
后面的就交给后台自己释放了。
记得加上
1
2thread.IsBackground=true;
现在交出完整核心代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29using System; using System.Threading; namespace dotnetcoreThreadInterrupt { class Application { public void Start() { Thread thread = new Thread(TtoRun); thread.IsBackground=true; thread.Start(); for (int i = 0; i < 6; i++) { Thread.Sleep(1000); } thread.Interrupt(); } public void TtoRun() { while (true) { Thread.Sleep(1000); System.Console.WriteLine("W"); } } } }
Gitbub链接: https://github.com/daolizhe/dotnetcoreThreadDemo
执行结果:记住是目录dotnetcoreThreadInterrupt直接dotnet run就能用,如果版本不对,改个版本
最后
以上就是友好酒窝最近收集整理的关于dotnetCore 线程中断问题dotnetCore 线程终止问题,我们用中断解决的全部内容,更多相关dotnetCore内容请搜索靠谱客的其他文章。
发表评论 取消回复