Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I have some classes that implement 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(){}

Is there a way to do this?

like image 226
CoolUserName Avatar asked Jul 13 '12 21:07

CoolUserName


People also ask

How do I run code in debugger?

Press F5 and hover over the type variable again. Repeat this step until you see a value of I in the type variable. Now, press F11 (Debug > Step Into or the Step Into button in the Debug Toolbar). F11 advances the debugger (and executes code) one statement at a time.

How do I get out of debug mode in Visual Studio?

To end a debugging session in Microsoft Visual Studio, from the Debug menu, choose Stop Debugging.

Can you step back in a debugger?

You can use step-back via the new Step Backward and Step Forward icons in the Debug toolbar. These icons navigate the events in the Events Tab. So, if you've just taken a step in live debugging (F10 or F11), you can use the Step Backward button to quickly navigate to the previous step.


2 Answers

For normal stopping of Windows services, you should put your code in your Stop method.

http://msdn.microsoft.com/en-us/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.

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. 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). 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. 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. 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. (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).

like image 177
James Manning Avatar answered Oct 13 '22 01:10

James Manning


Well, the CLR doesn't make any promises regarding when your objects are going to be collected or disposed-of.

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.
That's the only time when you're guaranteed when they'll be disposed of.

like image 26
J. Ed Avatar answered Oct 13 '22 01:10

J. Ed