public void threadMethod()
{
try
{
// do something
}
catch (ThreadInterruptedException e)
{
Console.WriteLine("[Net]", role, "Thread interrupted.");
n.CloseConnection();
}
finally
{
if (isAutenticated == false)
{
n.CloseConnection();
}
Dispatcher.Invoke(addMessage, "Network connection searching was disabled.");
DebuggerIX.WriteLine("[Net]", role, "Finished");
Dispatcher.Invoke(threadStoppedDel);
}
}
threadMethod的方法是通過System.Threading.Thread運行。線程可能會因此而中斷,因此,異常ThreadInterruptedException可能會在finally塊中拋出,對吧?我是否必須再次將試塊封入試塊中?如何正確處理ThreadInterruptedException?
謝謝!
處理它的最好方法是不導致它。不要使用異常來更改線程的流量控制。使用良好的交叉線程信號,例如'ManualResetEvent'。 – 2010-07-22 21:29:01