c# - WPF app, hosting exe with HwndHost, childapp closes itself, get invalid windows handle exception -
if have wpf app using host external executables. use system.diagnostics.process start application.
if hostapp controls close , can call hwndhost.dispose() before process.kill works well. destroywindowoverride gets called should , drops out.
if childapp closes itself, catching process.exited event. call dispose. don't immediate exception or everytime exception, win32exception "invalid windows handle" error. not call through destroywindowoverride
i've tried clearing datacontext, removing object visual tree. tried catch exception.
i using dwaneneed.hwndhostex (a derivative of hwndhost). expect using straight hwndhost same problem.
any thoughts?
@ ms.win32.unsafenativemethods.enablewindow(handleref hwnd, boolean enable)
at system.windows.interop.hwndhost.onenabledchanged(object sender, dependencypropertychangedeventargs e) @ system.windows.uielement.raisedependencypropertychanged(eventprivatekey key, dependencypropertychangedeventargs args) @ system.windows.uielement.onisenabledchanged(dependencyobject d, dependencypropertychangedeventargs e) @ system.windows.dependencyobject.onpropertychanged(dependencypropertychangedeventargs e) @ system.windows.frameworkelement.onpropertychanged(dependencypropertychangedeventargs e) @ system.windows.dependencyobject.notifypropertychange(dependencypropertychangedeventargs args) @ system.windows.dependencyobject.updateeffectivevalue(entryindex entryindex, dependencyproperty dp, propertymetadata metadata, effectivevalueentry oldentry, effectivevalueentry& newentry, boolean coercewithdeferredreference, boolean coercewithcurrentvalue, operationtype operationtype) @ system.windows.dependencyobject.coercevalue(dependencyproperty dp) @ system.windows.uielement.invalidateforceinheritpropertyonchildren(visual v, dependencyproperty property) @ system.windows.uielement.onisenabledchanged(dependencyobject d, dependencypropertychangedeventargs e) @ system.windows.dependencyobject.onpropertychanged(dependencypropertychangedeventargs e) @ system.windows.framew
the hwndhost had handler isenabledchanged event. in implementation there call unsafenativemethods.enablewindow(_hwnd, value) throw exception when child application closed (not everytime, often).
for context, able disconnect delegate called in response isenabledchanged event. doesn't appear need it. if did, can redirect implementation of handler own code , proper isalive check fix it.
private void clearisenabledevent() { fieldinfo handlerinfo = typeof(hwndhost).getfield("_handlerenabledchanged", bindingflags.instance | bindingflags.nonpublic); // can't this, hwndhost needs not null //handlerinfo.setvalue(this,null); // replace functionality own code (adjust\fix own context) //handlerinfo.setvalue(this, new dependencypropertychangedeventhandler(onenabledchanged)); // in case, doesn't appear implementation needed // empty delegate if (handlerinfo != null) handlerinfo.setvalue(this, new dependencypropertychangedeventhandler(delegate { })); }