有一个简单的方法来做到这一点(不是一个解决方法)
首先,您需要设置将火的时候,您的应用程序会死
// somewhere with global scope. On a singleton or in program class maybe
// this is set when you want to terminate your application
private static ManualResetEvent ExitWaitHandle = new ManualResetEvent(false);
一个事件,这是怎么了在其他地方使用它
// the event you want to check but it's blocking your application termination
private static AutoResetEvent yourEvent = new AutoResetEvent(true);
// the method where you have the problem
private static void FooAsync()
{
try
{
WaitHandle.WaitAny(new WaitHandle[]{yourEvent, ExitWaitHandle});
Checkpoint();
// other stuff here
// check if thread must die
Checkpoint();
}
catch(ApplicationTerminatingException)
{
// thread must die, do cleanup and finalization stuff here
}
catch(Exception)
{
// holy cow! what should we do?
}
}
private void CheckPoint()
{
// fast check if the exit handle is set
if(ExitWaitHandle.WaitOne(0))
{
throw new ApplicationTerminatingException(); // custom exception
}
}
唯一的开销是,“一些”代码后,你需要设置一个检查点,以中止你的线程。希望这是你正在寻找的。
嗯,你手上有一个讨厌的问题。终止调试会话是使用Windows中最大的枪TerminateProcess()完成的。 Taskmgr.exe中使用的同类枪。只有一种可能的方式是,这不会终止进程,内核线程繁忙并且不能完成I/O请求。这绝对不是一个AutoResetEvent。没有意义。 –
@Hans - 一旦连接调试器,我在调用堆栈中看到的WaitHandle不是信号的等待句柄,而是内核I/O任务的不同等待句柄?在这项任务中唯一的I/O是一些日志记录,这真的很简单(只是每隔一段时间手动触发一次,而不是以任何频率发生) – Matt
我所知道的是,你所描述的并不会使感。因此,如果没有任何文档,我会立即假定描述是错误的。发布堆栈跟踪与非托管调试和Microsoft符号服务器启用。 –