When someone uses the Task Manager to end a .NET process which has instantiated a COM object, AFAIK there is no way to call Marshal.FinalReleaseComObject in a try {} finally {}, using {} or event handling block.
The next time I fire up the application, the COM object is in an unusable state and I need to reboot the machine to make it work again.
I use Marshal.BindToMoniker to instantiate the object.
What am I doing wrong, is there anything I missed?
I tried Saeed’s suggestion but it didn’t work
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);
}
Task Manager is using the native equivalent of
Process.Kill(or possibly something even stronger). Note this narrative from the documentation:And,
In short, there’s nothing you can really do from inside your program code to protect against this – the plug has been pulled. No window messages are going to be sent.