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

推荐订阅源

N
News and Events Feed by Topic
WordPress大学
WordPress大学
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
L
LangChain Blog
雷峰网
雷峰网
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
NISL@THU
NISL@THU
Scott Helme
Scott Helme
量子位
S
Security Affairs
T
Threat Research - Cisco Blogs
博客园_首页
云风的 BLOG
云风的 BLOG
D
Docker
AWS News Blog
AWS News Blog
腾讯CDC
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
U
Unit 42
Recent Announcements
Recent Announcements
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
T
The Exploit Database - CXSecurity.com
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
L
Lohrmann on Cybersecurity
The Hacker News
The Hacker News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Y
Y Combinator Blog
S
Security @ Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Check Point Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
I
InfoQ

博客园 - 宏宇

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; //强制退出循环

    }

    //线程开始处理

    }

    }