简体   繁体   English

退出VisualStudio调试器时执行代码

[英]Execute code when VisualStudio debugger is exiting

I had assumed that when terminating debugging (such as by hitting the Stop button, or hitting Shift+F5), that any class implementing a finalizer or IDisposable would be, well, disposed. 我以为在终止调试时(例如,单击“停止”按钮,或单击Shift + F5),可以很好地处置实现finalizer或IDisposable任何类。

I have some classes that implement IDisposable . 我有一些实现IDisposable类。 There are a few things I'd like to (try) and do as the application exits from the debugger (or from crashing in production). 当应用程序从调试器退出(或从生产崩溃中退出)时,我想做一些尝试。 Right now, Dispose() does not appear to be called, nor a finalizer ~MyClass(){} 现在,似乎没有调用Dispose() ,也没有终结器~MyClass(){}

Is there a way to do this? 有没有办法做到这一点?

For normal stopping of Windows services, you should put your code in your Stop method. 为了正常停止Windows服务,应将代码放入Stop方法中。

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.stop.aspx http://msdn.microsoft.com/zh-CN/library/system.serviceprocess.servicebase.stop.aspx

In general, rude thread aborts and rude app domain unloads aren't going to run 'normal' finalizers - you can get more details in this MSDN article. 通常,粗鲁的线程中止和粗鲁的应用程序域卸载将不会运行“普通”终结器-您可以在此MSDN文章中获得更多详细信息。

https://web-beta.archive.org/web/20150423173148/https://msdn.microsoft.com/en-us/magazine/cc163716.aspx https://web-beta.archive.org/web/20150423173148/https://msdn.microsoft.com/en-us/magazine/cc163716.aspx

Up to this point, I've simply talked about thread aborts as the result of the runtime throwing ThreadAbortException on a thread. 到目前为止,我只是简单地谈到了线程中止,因为运行时在线程上抛出了ThreadAbortException。 Typically, this will cause the thread to terminate. 通常,这将导致线程终止。 However, a thread can handle a thread abort, preventing it from terminating the thread. 但是,线程可以处理线程中止,从而防止其终止线程。 To account for this, the runtime provides a more powerful action, the aptly named rude thread abort. 为了解决这个问题,运行时提供了更强大的操作,恰当地命名为粗鲁的线程中止。 A rude thread abort causes a thread to cease execution. 粗鲁的线程中止会导致线程停止执行。 When this happens, the CLR makes no guarantees that any back-out code on the thread will run (unless the code is executing in a CER). 发生这种情况时,CLR不能保证线程上的任何回退代码都将运行(除非代码在CER中执行)。 Rude, indeed. 的确是粗鲁的。

Similarly, while a typical application domain unload will gracefully abort all threads in the domain, a rude application domain unload will rudely abort all threads in the domain, and makes no guarantees that normal finalizers associated with objects in that domain will run. 类似地,尽管典型的应用程序域卸载将正常中止该域中的所有线程,但是粗鲁的应用程序域卸载将无礼地中止该域中的所有线程,并且不保证与该域中的对象相关联的普通终结器将运行。 SQL Server 2005 is one CLR host that makes use of rude thread aborts and rude application domain unloads as part of its escalation policy. SQL Server 2005是一台CLR主机,它利用粗暴的线程中止和粗暴的应用程序域卸载作为其升级策略的一部分。 When an asynchronous exception occurs, the resource allocation failure will be upgraded to a thread abort. 发生异步异常时,资源分配失败将升级为线程中止。 And when a thread abort occurs, if it doesn't finish within a time span set by SQL Server, it'll be upgraded to a rude thread abort. 而且,当发生线程中止时,如果它没有在SQL Server设置的时间范围内完成,它将升级为粗鲁的线程中止。 Similarly, if an application domain unload operation does not finish within a time span set by SQL Server, it'll be upgraded to a rude application domain unload. 同样,如果应用程序域卸载操作未在SQL Server设置的时间段内完成,它将升级为粗鲁的应用程序域卸载。 (Note that the policies just laid out are not exactly what SQL Server uses, as SQL Server also takes into account whether code is executing in critical regions, but more on that topic shortly). (请注意,刚刚列出的策略并不完全是SQL Server所使用的策略,因为SQL Server还考虑了代码是否在关键区域中执行,但不久之后将讨论更多内容)。

Well, the CLR doesn't make any promises regarding when your objects are going to be collected or disposed-of. 好吧,CLR不会就何时收集或处置您的物品做出任何承诺。

You can try calling the garbage collector explicitly, but I don't think that's a recommended approach. 您可以尝试显式调用垃圾收集器,但是我不建议这样做。

The best thing to do is use your IDisposable objects inside a using block. 最好的办法是在using块内使用IDisposable对象。
That's the only time when you're guaranteed when they'll be disposed of. 只有这样,您才能保证它们会被处置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM