Visual Studio:调试停止时执行清理代码 [英] Visual Studio : executing clean up code when debugging stops

查看:49
本文介绍了Visual Studio:调试停止时执行清理代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们开发了一个应用程序,它使用 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

这篇关于Visual Studio:调试停止时执行清理代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆