[英]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.