Check for hung Office process when using Office Automation

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-14 13:56:47

问题


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?


回答1:


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




回答2:


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!




回答3:


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.



来源:https://stackoverflow.com/questions/9905/check-for-hung-office-process-when-using-office-automation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!