2

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();
    }
}

}

  • 1
    I don't know what's missing, but there's not enough information here to answer the question. – Greg D May 14 '12 at 21:18
  • If you block the UI thread (as it appears you're doing here), the message pump won't run and you won't see the messages. – Dan Bryant Sep 20 '12 at 22:30
  • I don't think so. Call Thread.Sleep (or use any kind of block) then perform some ui input. You will see that, after sleep time, input will be properly processed. Most of time this happens with this sample code...but, sometimes, clicks over taskbar are missing (just them). This problem can may be related to [this one](http://stackoverflow.com/questions/2391589/wpf-window-stays-minimized-even-when-setting-windowstate-explicitly) – Marcio Barcellos Sep 21 '12 at 18:34

0 Answers0