惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
Recorded Future
Recorded Future
博客园 - 司徒正美
V
Vulnerabilities – Threatpost
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
D
Darknet – Hacking Tools, Hacker News & Cyber Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Jina AI
Jina AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News: Ask HN
Hacker News: Ask HN
S
Security Affairs
V
Visual Studio Blog
Schneier on Security
Schneier on Security
T
Tailwind CSS Blog
Martin Fowler
Martin Fowler
V2EX - 技术
V2EX - 技术
博客园 - Franky
S
Secure Thoughts
Blog — PlanetScale
Blog — PlanetScale
G
GRAHAM CLULEY
D
DataBreaches.Net
O
OpenAI News
Forbes - Security
Forbes - Security
云风的 BLOG
云风的 BLOG
Google Online Security Blog
Google Online Security Blog
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
T
Tenable Blog
Latest news
Latest news
N
News and Events Feed by Topic
博客园_首页
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
美团技术团队
T
The Exploit Database - CXSecurity.com
K
Kaspersky official blog
B
Blog
阮一峰的网络日志
阮一峰的网络日志
T
Threat Research - Cisco Blogs
SecWiki News
SecWiki News
PCI Perspectives
PCI Perspectives
GbyAI
GbyAI

博客园 - 宏宇

C#中一个简单的Task C#中多线程Task详解 C#数据去重的5种方式 Automa – AI浏览器扩展工具 超好用的网页端自动化扩展工具-Automa HTTP 请求“报头”——Referer 和 Cookie 使用vs2022将.net8的应用程序发布为一个单独文件 元数据、数据元、数据源、源数据 IIS设置使某IP访问时,跳转到指定页面 IIS自定义错误页面 关闭PerfWatson2.exe 体验改善计划 C# 线程(Thread) 配置 sql server 最大内存 sqlserver内存最佳配置 Windows安装时调出系统的cmd功能 Shift+F10 windows无法安装到这个磁盘,选中的磁盘采用gpt分区 如何删除硬盘efi系统分区 site命令 win10系统图片打开方式为照片查看器的方法 不安装运行时运行.NET程序
Thread.Abort的.Net Core替代方法
宏宇 · 2024-10-14 · via 博客园 - 宏宇

在使用.Net Framework的时候,我们一般用Thread.Start()来开始一个线程,用Thread.Abort()来强制结束这个线程,然而当项目整体迁移到.Net Core的时候,发现程序进行到Thread.Abort()的时候突然崩掉了。

        原因是Thread.Abort 已被弃用。那么结束一个线程的替代方法是什么呢。当尝试采用Thread.Interrupt()后,线程并没有按预期的情况结束,反而是随着线程的不断开启而越来越来以至于卡线程了。

        按照官方的解决方式是使用 CancellationToken 来中止工作单位的处理,而不是呼叫 Thread.Abort。 下列范例示范如何使用 CancellationToken。

    void ProcessPendingWorkItemsNew(CancellationToken cancellationToken)

    {

    if (QueryIsMoreWorkPending())

    {

    // If the CancellationToken is marked as "needs to cancel",

    // this will throw the appropriate exception.

    cancellationToken.ThrowIfCancellationRequested();



    WorkItem work = DequeueWorkItem();

    ProcessWorkItem(work);

    }

    }

​        然而本人觉得这个方法略显麻烦,何不从根本的线程运行逻辑上来中断线程,大家知道所有线程不外乎一个循环,我只要能够控制这个循环结束不就行了吗。

        处理方法如下:

        


    private bool isThreadStart = false; //增加一个线程控制开关

    private Thread thread;

    //线程启动方法

    void ThreadStart()

    {

    thread = new Thread(ThreadRun); //初始化一个ThreadRun的线程

    isThreadStart = true; //控制线程开始

    thread.Start();

    }

    //线程停止方法

    void ThreadStop()

    {

    isThreadStart = false; //控制线程结束

    thread = null;

    }

    //线程运行方法

    void ThreadRun()

    {

    while(true)

    {

    if(!isThreadStart)

    {

    break; //强制退出循环

    }

    //线程开始处理

    }

    }