-1
我使用了一个约束的执行区域(CER)保护的代码部分线程的while循环中:如何跳出循环中只包含一个约束的执行区域(CER)
private static void MyThreadFunc()
{
try {
...
while (true)
{
RuntimeHelpers.PrepareConstrainedRegions();
try { }
finally
{
// do something not to be aborted
}
Thread.Sleep(1); // allow while loop to be broken out
}
}
catch (ThreadAbortException e)
{
// handle the exception
}
}
问题是如果我在while循环结束时没有引入Thread.Sleep(1)
语句,则线程上任何调用Thread.Abort()的尝试都会挂起。有没有更好的方法可以在不使用Thread.Sleep()
函数的情况下中止线程?