在thread.Abort()之后会发生什么?
说我有:
Thread mWorker = new Thread(new ThreadStart(this.run));
..
mWorker.Start();
**where**
private void run()
{
Logger.d(TAG, "run()");
...
try {
while (this.mRunning){
...
}
} catch (ThreadAbortException tae){
Logger.e(TAG,"some msg", tae);
this.doSomething();
} catch (IOException ioe){
Logger.e(TAG,"some msg", ioe);
this.doSomething();
} catch (Exception e){
Logger.e(TAG,"some msg", e);
this.doSomething();
} finally {
gracefoulyClose();
}
Logger.d(TAG, "run() - ended");
}
Run Code Online (Sandbox Code Playgroud)
线程更复杂..但这里显示的是必要的.那么当Abort()被调用时会发生什么?我的捕获工作会继续doSomething()的调用吗?
因为我还在控制台收到:
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code
Run Code Online (Sandbox Code Playgroud)
但我确实有一个问题.不是吗?
来自doc:
当调用Abort方法来销毁线程时,公共语言运行库会抛出ThreadAbortException. ThreadAbortException是一个可以捕获的特殊异常,但它会在catch块的末尾自动再次引发.引发此异常时,运行时会在结束线程之前执行所有finally块.因为线程可以在finally块做一个无限的计算或致电Thread.ResetAbort取消中止,也不能保证该线程将永远结束.如果要等到中止的线程结束,可以调用Thread.Join方法.Join是一个阻塞调用,在线程实际停止执行之前不会返回.
换句话说,在ThreadAbortException执行catch块之后,异常会重新引发,因此您的上一个记录器行(例如Logger.d(TAG, "run() - ended"))永远不会执行.但由于呼叫this.doSoemthing是在为catch块ThreadAbortException,它会执行.
另请注意,您的finally块确实执行(请参阅上面的doc).
| 归档时间: |
|
| 查看次数: |
12657 次 |
| 最近记录: |