在WPF上缺少WM_Messages

我通过单击Windows任务栏来最小化/恢复我的wpf应用程序时遇到问题。 有时它有效,有时则不然。 所以, 我在app主窗口添加了一个钩子 ,试图看看事件是否来了。 有时他们这样做,有时他们没有。 然后我使用Spy ++来查看事件是否至少被启动……是的! 他们是。 那我为什么只收到其中一些?

public MyMainWindow() { InitializeComponent(); this.SourceInitialized += new EventHandler(OnSourceInitialized); } void OnSourceInitialized(object sender, EventArgs e) { HwndSource source = (HwndSource)PresentationSource.FromVisual(this); source.AddHook(new HwndSourceHook(HandleMessages)); } private IntPtr HandleMessages(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { switch (msg) { case 0x0112://WM_SYSCOMMAND: switch ((int)wParam & 0xFFF0) { case 0xF020://SC_MINIMIZE: break; case 0xF120://SC_RESTORE: break; } break; } } 

我有一个自定义主 ,我正在使用Caliburn Micro和自定义Bootstrapper 。


我想我做了一个简化的案例,可以看到这个问题(不确定这是我的问题的相同来源)。 我使用Timer来模拟等待异步socket / activeX响应。 可以看到,单击任务栏,有时窗口将不会最大化/最小化。

还是不知道怎么解决它。

 using System; using System.Threading; using System.Windows; using System.Windows.Threading; using Timer = System.Timers.Timer; namespace Garbage { ///  /// Interaction logic for MainWindow.xaml ///  public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var dispatcherTimer = new DispatcherTimer { Interval = new TimeSpan(0,0,0,0,10) }; dispatcherTimer.Tick += (o, e) => { var waintingForSocketAsyncRenponseEmulation = new Timer(10); waintingForSocketAsyncRenponseEmulation.Elapsed += delegate { lock (this) { Monitor.Pulse(this); } }; waintingForSocketAsyncRenponseEmulation.Start(); lock (this) { Monitor.Wait(this); } }; dispatcherTimer.Start(); } } 

}