Visual Studio中:在执行调试停止时清理code

问题描述:

我们开发了一个使用Excel的互操作库的应用程序(的Microsoft.Office.Interop.Excel)来读取某些Excel文件。

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

当一个问题出现在应用程序中,事件Application.ThreadException的处理,所以资源被释放(关闭Excel ...)。

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

问题是,当我们使用VS调试器,如果我们停止执行(因为异常,或者断点的过程中休息,有很多的原因,我们会做到这一点),资源不释放和Excel保持打开。
当然,应用程序启动下一次......它崩溃,因为有门锁上的文件。

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.

所以我在寻找一种方法来强制Excel对象的释放,即使与调试器停止。

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

任何建议?

您可以使用DTE(的VisualStudio自动化模式)时,停止调试情况,下面是这个想法的片段来写会被调用宏。

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