WPF应用程序,使用HwndHost托管exe,子应用程序关闭自身,获取无效窗口句柄异常



如果有一个WPF应用程序,我正在使用它来托管外部可执行文件。我使用System.Diagnostics.Process来启动应用程序。

如果hostApp控制关闭,并且可以在process.kill之前调用HwndHost.Dispose(),那么它运行良好。DestroyWindowOverride被调用,所有内容都会退出。

如果子应用程序自行关闭,则我正在捕获Process.Exited事件。我立即打电话给处理人员。我不会立即得到异常或每次都得到异常,但我经常会得到带有"无效窗口句柄"错误的win32Exception。我没有通过DestroyWindowOverride 接到电话

我已经尝试清除DataContext,从可视化树中删除该对象。尝试捕获异常。

我使用的是DwaneNeed.HwndHostEx(HwndHost的衍生产品)。我预计使用直接HwndHost也会遇到同样的问题。

有什么想法吗?

at MS.Win32.UnsafeNativeMethods.EnableWindow(HandleRef hWnd, Boolean enable)

位于System.Windows.Interop.HwndHost.OnEnabledChanged(对象发送器,DependencyPropertyChangedEventArgs e)位于System.Windows.UIElement.RiseDependencyPropertyChanged(EventPrivateKey密钥,DependencyProperty ChangedEventArgs参数)位于System.Windows.UIElement.OnIsEnabledChanged(DependencyObject d,DependencyProperty ChangedEventArgs e)位于System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)位于System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)位于System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs参数)在System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex EntryIndex、DependencyProperty dp、PropertyMetadata元数据、EffectiveValueEntry oldEntry、EffectiveValue Entry和newEntry、布尔强制DeferredReference、布尔强制CurrentValue、OperationType OperationType)位于System.Windows.DependencyObject.CorceValue(DependencyProperty-dp)位于System.Windows.UIElement.InvalidateForceInheritPropertyOnChildren(Visual v,DependencyProperty属性)位于System.Windows.UIElement.OnIsEnabledChanged(DependencyObject d,DependencyProperty ChangedEventArgs e)位于System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)在System.Windows.Framew

HwndHost有一个IsEnabledChanged事件的处理程序。在实现中,调用了UnsafeNativeMethods.EnableWindow(_hwnd,value),它会在子应用程序关闭时抛出异常(不是每次都调用,而是经常调用)。

在这种情况下,我能够断开响应IsEnabledChanged事件而调用的委托。似乎我不需要它。如果我需要,我可以将处理程序的实现重定向到我自己的代码,并通过适当的IsAlive检查修复它。

private void ClearIsEnabledEvent()
{
FieldInfo handlerInfo = typeof(HwndHost).GetField("_handlerEnabledChanged", BindingFlags.Instance | BindingFlags.NonPublic);
// can't do this, HwndHost needs it to be NOT null
//handlerInfo.SetValue(this,null);
// replace functionality with my own code (adjustfix for my own context)
//handlerInfo.SetValue(this, new DependencyPropertyChangedEventHandler(OnEnabledChanged));
// in this case, it doesn't appear anything implementation is needed
// just an empty delegate
if (handlerInfo != null)
handlerInfo.SetValue(this, new DependencyPropertyChangedEventHandler(delegate { }));
}

最新更新