Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio : executing clean up code when debugging stops

We developed an application that uses Excel interop libraries (Microsoft.Office.Interop.Excel) to read some Excel files.

When a problem occur in the application, the event Application.ThreadException is handled, so the resources are released (Excel is closed...).

The problem is that when we use the VS debugger, if we stop the execution (because the process breaks on an exception, or a breakpoint, there are lots of reasons why we'd do that), the resources are not released and Excel stays opened. And of course, next time the application is launched... it crashes because there are locks on the file.

So I'm looking for a way to force the release of the Excel objects, even when stopped with the debugger.

Any suggestion ?

like image 805
Julien N Avatar asked Jun 23 '09 15:06

Julien N


People also ask

How do I keep Debug code in Visual Studio?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

How do I force stop debugging in Visual Studio?

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

Why are my breakpoints not working?

If a source file has changed and the source no longer matches the code you're debugging, the debugger won't set breakpoints in the code by default. Normally, this problem happens when a source file is changed, but the source code wasn't rebuilt. To fix this issue, rebuild the project.

How do I turn off just my code in VS code?

To enable or disable Just My Code in Visual Studio, under Tools > Options (or Debug > Options) > Debugging > General, select or deselect Enable Just My Code.


2 Answers

You can use the DTE (VisualStudio Automation Model) to write a macro that will be invoked when a stop debug happens, below is a snippet of the idea.

Private Sub DebuggerEvents_OnEnterBreakMode(
   ByVal Reason As EnvDTE.dbgEventReason, 
   ByRef ExecutionAction As EnvDTE.dbgExecutionAction) Handles DebuggerEvents.OnEnterBreakMode
    If (Reason = dbgEventReason.dbgEventReasonStopDebugging) Then
        // DO YOUR CLEAN UP CODE HERE
    End If
End Sub
like image 69
Shay Erlichmen Avatar answered Oct 25 '22 18:10

Shay Erlichmen


Unfortunately there isn't a way to do this. The stop button in visual studio kills the process, so it doesn't have any chance to clean up.

As a possible way round your problem (although not a very good one), you could write a cleanup routine and execute it manually from the immediate window before stopping the app.

[Edit: Ignore me. This answer is wrong. Shay Erlichmen has come up with a much better solution using a macro]

like image 42
Simon P Stevens Avatar answered Oct 25 '22 19:10

Simon P Stevens