這不是我對這個問題的理想解決方案,但現在,它的工作原理...
當一個對話窗口,在您要關閉文檔的時間開放,您可以搭乘收到COMException和閱讀HResult ID來控制對異常做出反應的方式。這是我發現的Open Dialog問題以及Word意外關閉的結果。
catch (System.Runtime.InteropServices.COMException ex)
{
switch (ex.HResult)
{
case -2147417846: //Dialog box is open and blocking us
context.Clients.All.addMessage("CloseWord", "Can't close Word, please check for open dialog box");
return;
case -2147023174: //Word Instance died without us knowing, need to set back to null to recover
context.Clients.All.addMessage("CloseWord", "Word Failed, attempting to recover...");
break;
default: //this is to catch the unknown and bubble up the details
context.Clients.All.addMessage(
string.Format("CloseWord", "Oops... Something went wrong Code {0} Message: {1}", ex.HResult, ex.Message));
return;
}
}