Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PushFrame locks up WPF window when user is moving window

Tags:

c#

.net

wpf

I'm using PushFrame to ensure that my window finishes drawing before executing additional code. My application has some time sensitive functions that require the windows have been updated before I continues executing code.

So I'm using the sample from msdn: http://msdn.microsoft.com/en-us/library/vstudio/system.windows.threading.dispatcher.pushframe(v=vs.110).aspx

Which works great, except, if a user is dragging my window when this code executes the window hangs and you can only get it back with a ctrl-alt-del.

Any ideas?

like image 619
Joel Barsotti Avatar asked Oct 16 '13 19:10

Joel Barsotti


2 Answers

The application seems freezed because mouse capture is not automatically released from the window resize or DragMove() operation after the Dispatcher.PushFrame() is called from the user code.

The workaround would be to manually release mouse capture from any window in the application that is capturing the mouse prior to calling Dispatcher.PushFrame():

        ...
        if (priority < DispatcherPriority.Loaded)
        {
            IntPtr capturingHandle = GetCapture();
            for (int i = 0; i < Application.Current.Windows.Count; i++)
            {
                if (new WindowInteropHelper(
                                            Application.Current.Windows[i]
                                           ).Handle == capturingHandle)
                {
                    Mouse.Capture(
                                  Application.Current.Windows[i],
                                  CaptureMode.Element
                                 );
                    Application.Current.Windows[i].ReleaseMouseCapture();
                    break;
                }
            }
        }
        ...

This workaround utilizes the GetCapture() p/invoke declaration:

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetCapture();
like image 117
fvojvodic Avatar answered Oct 30 '22 04:10

fvojvodic


Unfortunately I do not have a solution for you but can only confirm we can reproduce this too in our application (and in a 50 line example program). Feel free to vote for this connect issue.

like image 31
Hrvoje Prgeša Avatar answered Oct 30 '22 02:10

Hrvoje Prgeša