STA线程中止异常

Ank*_*Roy 7 c# multithreading

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

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();
}
Run Code Online (Sandbox Code Playgroud)

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

private void btnStop_Click(object sender, EventArgs e)
{

 if (Test != null)
         Test .Abort();
}
Run Code Online (Sandbox Code Playgroud)

这是给以下异常:"类型'System.Threading.ThreadAbortException’出现在mscorlib.dll线程0x13dc的第一个机会异常已退出,代码为0(为0x0)."

如何摆脱这种异常?

Vin*_*B R 2

ThreadAbort 异常应该不是问题。未处理的 ThreadAbortException 是仅有的两种不会导致应用程序关闭的异常之一(另一种是 AppDomainUnloadException)。

将其包装在 try catch 中并处理 ThreadAbort 类型的异常并设置Thread.ResetAbort = true;

检查此链接了解更多详细信息。