简体   繁体   English

STA线程中止异常

[英]STA thread Abort Exception

I am initializing a thread as static thread as shown below 我正在将线程初始化为静态线程,如下所示

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();
}

but when I am aborting this thread via this method 但是当我通过这种方法中止这个线程时

private void btnStop_Click(object sender, EventArgs e)
{

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

It is giving following exception : " A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll The thread 0x13dc has exited with code 0 (0x0). " 它给出了以下异常:“mscorlib.dll中出现'System.Threading.ThreadAbortException'类型的第一次机会异常。线程0x13dc已退出,代码为0(0x0)。”

How to get rid of this exception?? 如何摆脱这种异常?

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

The ThreadAbort Exception should not be a problem. ThreadAbort异常应该不是问题。 An unhandled ThreadAbortException is one of only two types of exception that does not cause application shutdown (the other is AppDomainUnloadException). 未处理的ThreadAbortException是仅导致应用程序关闭的两种异常类型之一(另一种是AppDomainUnloadException)。

wrap it in a try catch and handle exception of type ThreadAbort and set Thread.ResetAbort = true; 将它包装在try catch中并处理ThreadAbort类型的异常并设置Thread.ResetAbort = true;

Check this link for more details. 查看此链接了解更多详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM