简体   繁体   English

Visual Studio:在调试停止时执行清理代码

[英]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. 我们开发了一个使用Excel互操作库(Microsoft.Office.Interop.Excel)来读取一些Excel文件的应用程序。

When a problem occur in the application, the event Application.ThreadException is handled, so the resources are released (Excel is closed...). 当应用程序中出现问题时,将处理事件Application.ThreadException,因此将释放资源(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. 问题是,当我们使用VS调试器时,如果我们停止执行(因为进程在异常或断点上中断,有很多原因导致我们这样做),资源不会被释放而Excel会停留打开。 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. 所以我正在寻找一种强制释放Excel对象的方法,即使在使用调试器停止时也是如此。

Any suggestion ? 有什么建议吗?

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. 您可以使用DTE(VisualStudio自动化模型)编写将在停止调试发生时调用的宏,下面是该想法的片段。

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

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. visual studio中的停止按钮会终止进程,因此没有任何机会进行清理。

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] Shay Erlichmen使用宏创建了一个更好的解决方案]

One possibility is to switch to a pure .NET solution such as SpreadsheetGear for .NET to get away from the performance and reliability problems associated with COM interop. 一种可能性是切换到纯.NET解决方案,例如SpreadsheetGear for .NET,以摆脱与COM互操作相关的性能和可靠性问题。

Disclaimer: I own SpreadsheetGear LLC 免责声明:我拥有SpreadsheetGear LLC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当执行SqlCommand.ExecuteScalar()时,Visual Studio停止调试 - Visual Studio stops debugging when SqlCommand.ExecuteScalar() is executed 在Visual Studio中使用SQLCE进行调试之前先清理解决方案 - Clean solution before debugging in Visual Studio with SQLCE Visual Studio停止调试统一项目 - Visual Studio stops debugging unity project 在Visual Studio 2010中调试源代码时发出的警告 - Warning given when debugging source code in visual studio 2010 调试visual studio代码时,launch.json中的'program'出错 - Error for 'program' in launch.json when debugging visual studio code 在 Visual Studio 中执行 C# 代码时出现控制台问题 - Issue with console when executing C# code in Visual Studio 在Visual Studio 2010中调试停止后自动运行批处理文件吗? - Automatically Run Batch File After Debugging Stops in Visual Studio 2010? Visual Studio Code需要显式清理和构建 - Visual Studio Code needs explicit clean and build Visual Studio调试器在混合调试模式下停止命中断点 - Visual Studio debugger stops hitting breakpoints in mixed debugging mode Visual Studio 2019 Edge Browser for javascript 调试停止在错误的行 - visual studio 2019 Edge Browser for javascript debugging stops at wrong line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM