Skip to content

Get InvalidOperationException: Dispatcher processing has been suspended, but messages are still being processed. #54

Closed
christianhuening opened this Issue Mar 14, 2013 · 2 comments

2 participants

@christianhuening

Hi there,

Had a nasty error and finally found a solution:

ErrorDescription:

We have a ListView with multiple Items, which can be edited by simply clicking onto them. By clicking them they change from TextBox to EditBox. Now when you first RightClick on of them, so that the usual contextmenu shows up and then perform a LeftClick on the TextBox so that it changes to edit mode, you get an InvalidOperationException which says Dispatcher processing has been suspended, but messages are still being processed.

The Problem here is, that we are interferring with WPF doing layout to change from TextBox to EditBox Mode and that layout in WPF runs under disable processing. See here for details: http://blogs.msdn.com/b/nickkramer/archive/2006/05/06/591252.aspx

Solution Description

So to solve that problem I dispatch the call to DragDrop.DoDragDrop in the DragSource_PreviewMouseMove() Method so that it runs async. When message processing is disabled, e.g. through layout rendering, then this will result in an InvalidOperationException, which in this case is thrown just once and very intentionally (as the link above points out). So we can safely catch this one.
For dispatching I needed to put DoDragDrop into its own method and do NotNull-Check for m_DragInfo since it might get executed while m_DragInfo is already null again.

Maybe someone has a better idea for this?

Solution Code from DragDrop.cs

Finally the code:

        private static void DragSource_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (m_DragInfo != null && !m_DragInProgress)
            {
                var dragStart = m_DragInfo.DragStartPosition;
                var position = e.GetPosition(null);

                if (Math.Abs(position.X - dragStart.X) > SystemParameters.MinimumHorizontalDragDistance ||
                    Math.Abs(position.Y - dragStart.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    var dragHandler = GetDragHandler(m_DragInfo.VisualSource);

                    if (dragHandler != null)
                    {
                        dragHandler.StartDrag(m_DragInfo);
                    }
                    else
                    {
                        DefaultDragHandler.StartDrag(m_DragInfo);
                    }

                    if (m_DragInfo.Effects != DragDropEffects.None && m_DragInfo.Data != null)
                    {
                        var data = new DataObject(DataFormat.Name, m_DragInfo.Data);

                        try
                        {
                            m_DragInProgress = true;
                            Application.Current.Dispatcher.Invoke(
                                DispatcherPriority.Normal,
                                new System.Threading.ParameterizedThreadStart(DoDragDrop),
                                data);
                        }
                        catch (InvalidOperationException invalidOperationException)
                        {
                            // don't do anything in that case
                        }
                        finally
                        {
                            m_DragInProgress = false;
                        }

                        m_DragInfo = null;
                    }
                }
            }
        }

        private static void DoDragDrop(object parameter)
        {
            if (m_DragInfo != null)
            {
                System.Windows.DragDrop.DoDragDrop(m_DragInfo.VisualSource, parameter, m_DragInfo.Effects);
            }
        }
@punker76
Owner

hi,

sorry for my late response.

first i think it's the same issue like #22 .
can you take a look at this, if the e.GetPosition(null) got the wrong position?
if think the left click should not do a dragdrop...

best regards

jan

@christianhuening
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.