I'm having problems to minimize/restore my wpf app by clicking on windows taskbar. Sometimes it works, sometimes it does not. So, I added a hook to app main window to try to see if events were coming or not. Sometimes they do, sometimes they do not. Then I use Spy++ to see if events were at least launched... Yes! They are. So why am I receiving just some of them?
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;
}
}
I have a custom main and I'm using Caliburn Micro with customized Bootstrapper.
I think I made a simplified case where this problem can be seen (not sure about this been same source of my problem). I used a Timer to emulate been waiting for async socket/activeX response. As is possible to see, clicking over taskbar, sometimes window will not be maximized/minimized.
Still don't know how to solve it.
using System;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using Timer = System.Timers.Timer;
namespace Garbage
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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();
}
}
}