正确处理在C#中的COM互操作对象特别是MS Office应用程序 [英] Proper disposal of COM interop objects in C# particularly MS Office applications

查看:166
本文介绍了正确处理在C#中的COM互操作对象特别是MS Office应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展严重依赖于多个Microsoft Office产品包括Access,Excel和Word中,其中包括PowerPoint,Outlook等应用程序。虽然做研究,互操作我发现开始VS2010和.NET 4,我们值得庆幸不再需要经过初步评估的噩梦。

I am developing an application that relies heavily on multiple Microsoft Office products including Access, Excel, Word, PowerPoint and Outlook among others. While doing research on interop I found out that starting with VS2010 and .NET 4, we thankfully no longer have to go through the nightmares of PIAs.

此外,我一直在阅读了大量的有关正确处理对象的物品,最明智的人似乎是之一。

Furthermore, I have been reading a lot of articles on proper disposal of objects, the most sensible one seemed to be this one.

不过,这篇文章是5岁,有没有关于这个问题AFAIK许多权威刊物。这里是code从上面的链接的例子:

However, the article is 5 years old and there are not many authoritative publications on the subject AFAIK. Here is a sample of code from the above link:

' Cleanup:
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()

Marshal.FinalReleaseComObject(worksheet)

oWB.Close(SaveChanges:=False)
Marshal.FinalReleaseComObject(workbook)

oApp.Quit()
Marshal.FinalReleaseComObject(application)

我想知道的是用今天的标准,如何准确的是这一点,我应该怎么看出来的,如果我希望支持我的申请,未来几年?

What I want to know is by today's standards, how accurate is this and what should I look out for if I expect to support my application for the coming few years?

更新:一个链接到一些可信的文章将是非常美联社preciated。顺便说一下,这不是一个服务器端应用程序。这将是计算机实验室,我们有用户,我们实例化他们的办公产品的交互运行。

UPDATE: A link to some credible articles would be highly appreciated. By the way, this is not a server side application. This will be running in computer labs where we have users interact with office products that we instantiate for them.

发现: 三部分组成的文章可能是最接近权威账户我希望找到。

FOUND IT: This three-part article is probably the closest to an authoritative account I would expect to find.

推荐答案

根据办公室的你控制通过互操作,并在服务器上发生的情况下,是的,你可能需要做一些认真的回旋得到的版本摆脱了Office应用程序的运行实例的。有未经predictable(和不必要的)对话当中,例如,Word将打开的时候它不是交互(无的Office应用程序特别适用于无人参与的执行,因此建议,即使是最新的版本,他们没有安装服务器端使用)。

Depending on the version of Office you are controlling through interop, and the circumstances that occur on the server, yes, you may need to do some serious convolutions to get rid of a running instance of one of the Office applications. There are unpredictable (and unnecessary) dialogs which, for example, Word will open when it is not interactive (None of the Office applications are particularly well designed for unattended execution, thus the recommendation that even with the latest versions that they not be installed for server-side use).

有关服务器的情况下,可以考虑使用的Aspose的筹码,而不是办公室或其他的替代品。互动,本地使用的情况下,可能需要上一次额外轰轰烈烈的杀戮,因为办公自动化服务器特别顽劣的非托管对象。

For server cases, consider using Aspose's stack rather than Office, or another of the alternatives. The interactive, local use cases, you may need that "extra vigorous killing" on occasion since the Office automation servers are particularly badly behaved unmanaged objects.

这篇关于正确处理在C#中的COM互操作对象特别是MS Office应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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