Visual Studio: performing code cleanup while stopping debugging

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

When a problem occurs, the Application.ThreadException event is processed, so the resources are freed (Excel is closed ...).

The problem is that when we use the VS debugger, if we stop execution (because the process breaks into an exception or breakpoint, there are many reasons why we do this), the resources will not be released and Excel remains open. And, of course, the next time the application is launched ... it crashes because there are locks in the file.

So I'm looking for a way to force the release of Excel objects, even if it is stopped using the debugger.

Any suggestion?

+7
debugging c # visual-studio
Jun 23 '09 at 15:47
source share
3 answers

You can use the DTE (VisualStudio automation model) to record a macro that will be called when the debugging stops, below is a snippet of ideas.

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 
+14
Jun 23 '09 at 15:59
source share

Unfortunately, there is no way to do this. The stop button in the visual studio kills the process, so it has no way to clear.

As a possible circle around your problem (although not very good), you can write a cleaning procedure and execute it manually from a direct window before stopping the application.

[Edit: Ignore me. This answer is incorrect. Shay Erlichmen came up with a much better solution using a macro.

+1
Jun 23 '09 at 15:54
source share

One option is to upgrade to a clean .NET solution, such as SpreadsheetGear for .NET , to get rid of performance and reliability issues with COM interoperability.

Disclaimer: I have SpreadsheetGear LLC

+1
Jun 23 '09 at 18:13
source share



All Articles