我是靠谱客的博主 友好酒窝,这篇文章主要介绍dotnetCore 线程中断问题dotnetCore 线程终止问题,我们用中断解决,现在分享给大家,希望可以做个参考。

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 不支持此成员。。。。。。,六个句号献给真香。
ABORT
那么很多小伙伴还要用终止怎么办呢?
既然不不能终止,我们就中断吧:
dangdangdangdangdangdang
:
Interrupt();
使用截图如下:(执行6秒就中断的截图)
执行6秒就中断的截图

官方参考方法
https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.thread.interrupt?view=netcore-2.1#System_Threading_Thread_Interrupt

后面的就交给后台自己释放了。
记得加上

复制代码
1
2
thread.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
29
using 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

Github链接截图

执行结果:记住是目录dotnetcoreThreadInterrupt直接dotnet run就能用,如果版本不对,改个版本
执行结果

最后

以上就是友好酒窝最近收集整理的关于dotnetCore 线程中断问题dotnetCore 线程终止问题,我们用中断解决的全部内容,更多相关dotnetCore内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部