检测 Excel 是否已退出并做出适当响应的正确方法?

Proper way to detect if Excel has been quit and respond appropriately?

我有一个 Windows 表单应用程序可以打开 Excel 的一个实例。如果用户提前退出应用程序,我想确保此 Excel 实例已退出。如果实例不是 null,我尝试调用 Excel 实例的 Quit() 方法,如下所示:

static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 form1 = new Form1();
            Application.Run(form1);
            form1.logFile.close();
            if (form1.xlApp != null)
            {
                form1.xlApp.Quit();
            }
        }
    }

但是如果我在任务管理器中退出 Excel 应用程序然后关闭 GUI,我会得到这个错误:

System.Runtime.InteropServices.COMException (0x800706BA): 
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

立即 window 中的 (xlApp==null) 检查表明 xlApp 不为空,即使没有 Excel 运行 的实例。

所以我尝试删除 Quit() 行并改用它:

Marshal.ReleaseComObject(form1.xlApp);

当用户提早退出 Excel 应用程序时,这可以很好地关闭而不会出错,但是如果在关闭 GUI 时该实例仍在任务管理器中 运行,ReleaseComObject() 调用不会结束 Excel 实例。

您的应用程序实例只知道 COM 对象准备好响应函数调用的内存地址。因此,当您终止进程时,它不会注意到。 对该对象的任何后续调用都会引发 RPC_E_DISCONNECTED 或 RPC_SERVER_UNAVAILABLE 作为 hResult。 我们尝试从应用程序中调用一些简单的东西,例如 Ready() 或 Caption()。如果它 returns 这些代码之一,我们知道它已断开连接。否则,发送消息是安全的——比如 Quit()