gpt4 book ai didi

c# - STA 线程中止异常

转载 作者:太空狗 更新时间:2023-10-29 20:29:08 26 4
gpt4 key购买 nike

我正在将一个线程初始化为静态线程,如下所示

Thread GenerateKeywords;
private void btnStart_Click(object sender, EventArgs e)
{

//Initializes the Test Thread
Test = new Thread(TestMethod);

//Sets the apartment state to Static
Test.SetApartmentState(ApartmentState.STA);

//Starts the GenerateKeywords Thread
Test.Start();
}

但是当我通过这个方法中止这个线程时

private void btnStop_Click(object sender, EventArgs e)
{

if (Test != null)
Test .Abort();
}

它给出了以下异常:“mscorlib.dll 中出现类型为“System.Threading.ThreadAbortException”的第一次机会异常线程 0x13dc 已退出,代码为 0 (0x0)。"

如何摆脱这个异常??

最佳答案

您应该在运行线程时轮询某些条件以中止它。在按钮停止时设置一些 bool 变量,然后在线程方法中轮询它以中止它。

关于c# - STA 线程中止异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3759317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com