使用 Office Automation 时检查挂起的 Office 进程

发布于 2024-07-04 18:59:58 字数 97 浏览 9 评论 0原文

有没有办法在使用 Office Automation 时检查 Microsoft Office 进程(即 Word、Excel)是否挂起? 另外,如果进程挂起,有没有办法终止它?

Is there a way to check to see if an Microsoft Office process (i.e. Word, Excel) has hung when using Office Automation? Additionally, if the process is hung, is there a way to terminate it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑凤梨 2024-07-11 18:59:58

首先我要说的是,我不建议在服务器上的服务中执行此操作,但我会尽力回答这些问题。

作为服务运行使得清理变得困难。 例如,您作为服务运行的内容可以杀死挂起的单词或 Excel。 您可能不得不终止该服务。 如果word或excel处于这种状态,你的服务会停止吗?

尝试测试它是否挂起的一个问题是,您的测试可能会导致一个新的 word 实例启动并工作,而该服务正在运行的实例仍然会挂起。

确定它是否挂起的最佳方法是要求它执行应该执行的操作并检查结果。 我需要更多地了解它实际上在做什么。

以下是在批处理文件中用于清理的一些命令(两者都应位于路径中):

  • sc stop servicename - 停止名为 servicename 的服务
  • sc start servicename - 启动名为 servicename 的服务
  • sc query servicename - 查询 servicename 的状态

  • taskkill /F /IM excel.exe - 终止所有excel.exe实例

Let me start off saying that I don't recommend doing this in a service on a server, but I'll do my best to answer the questions.

Running as a service makes it difficult to clean up. For example with what you have running as a service survive killing a hung word or excel. You may be in a position to have to kill the service. Will your service stop if word or excel is in this state.

One problem with trying to test if it is hung, is that your test could cause a new instance of word to startup and work, while the one that the service is running would still be hung.

The best way to determine if it's hung is to ask it to do what it is supposed to be doing and check for the results. I would need to know more about what it is actually doing.

Here are some commands to use in a batch file for cleaning up (both should be in the path):

  • sc stop servicename - stops service named servicename
  • sc start servicename - starts service named servicename
  • sc query servicename - Queries the status of servicename

  • taskkill /F /IM excel.exe - terminates all instances of excel.exe

风追烟花雨 2024-07-11 18:59:58

我记得几年前这样做过 - 所以我说的是 Office XP 或 2003 年,而不是 2007 年。

显然,现在更好的自动化解决方案是使用新的 XML 格式,该格式使用 System.IO.Packaging 命名空间来描述 docx 等。

当时,我常常注意到,每当 MSWord 寿终正寝并受够了时,计算机上就会运行一个名为“Dr. Watson”的进程。 这是我发现 Word 绊倒并摔倒的第一个线索。 有时我可能会看到多个WINWORD.EXE,但我的代码只是用来扫描好医生。 当我看到这一点(在代码中)时,我杀死了好医生本人的所有WINWORD.EXE进程,并重新启动了折磨Word的过程:-)

希望这能给你一些关于要寻找什么的线索。

祝一切顺利,

罗布·G·

P.S. 如果你不来的话,我什至可以在我的档案中挖出代码!

I remember doing this a few years ago - so I'm talking Office XP or 2003 days, not 2007.

Obviously a better solution for automation these days is to use the new XML format that describes docx etc using the System.IO.Packaging namespace.

Back then, I used to notice that whenever MSWord had kicked the bucket and had had enough, a process called "Dr. Watson" was running on the machine. This was my first clue that Word had tripped and fallen over. Sometimes I might see more than one WINWORD.EXE, but my code just used to scan for the good Doctor. Once I saw that (in code), I killed all WINWORD.EXE processes the good Doctor himself, and restarted the process of torturing Word :-)

Hope that gives you some clues as to what to look for.

All the best,

Rob G

P.S. I might even be able to dig out the code in my archives if you don't come right!

怪我闹别瞎闹 2024-07-11 18:59:58

后半部分我可以回答; 如果您在代码中引用了应用程序对象,则可以简单地对其调用“退出”:

private Microsoft.Office.Interop.Excel.Application _excel;
// ... do some stuff ...
_excel.Quit();

为了检查挂起的进程,我猜您会想要尝试从应用程序中获取一些数据,看看是否您会在合理的时间范围内获得结果(检查计时器或其他线程或其他东西)。 不过可能有更好的方法。

I can answer the latter half; if you have a reference to the application object in your code, you can simply call "Quit" on it:

private Microsoft.Office.Interop.Excel.Application _excel;
// ... do some stuff ...
_excel.Quit();

For checking for a hung process, I'd guess you'd want to try to get some data from the application and see if you get results in a reasonable time frame (check in a timer or other thread or something). There's probably a better way though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文