gpt4 book ai didi

c# - 如何恢复未正确释放的 COM 对象?

转载 作者:可可西里 更新时间:2023-11-01 09:30:38 25 4
gpt4 key购买 nike

当有人使用任务管理器结束已实例化 COM 对象的 .NET 进程时,AFAIK 无法在 try {} finally {} 中调用 Marshal.FinalReleaseComObject 使用 {} 或事件处理 block 。

下次启动应用程序时,COM 对象处于不可用状态,我需要重新启动机器才能使其再次运行。

我使用 Marshal.BindToMoniker 实例化对象。

我做错了什么,我错过了什么吗?


我尝试了 Saeed 的建议,但没有用

private const int WM_CLOSE = 0x0010;

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CLOSE)
{
logger.Info("WM_CLOSE");
Marshal.FinalReleaseComObject(comObject);
}

base.WndProc(ref m);
}

最佳答案

任务管理器正在使用 Process.Kill 的 native 等效项(或者可能更强大)。请注意文档中的这段叙述:

Kill forces a termination of the process, while CloseMainWindow only requests a termination.

...

Calling CloseMainWindow sends a request to close to the main window, which, in a well-formed application...

还有,

Data edited by the process or resources allocated to the process can be lost if you call Kill. Kill causes an abnormal process termination and should be used only when necessary.

简而言之,您无法从程序代码内部真正做任何事情来防止这种情况发生——插头已被拔掉。不会发送任何窗口消息。

关于c# - 如何恢复未正确释放的 COM 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12047005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com