25

I Have a WPF Project, When i try to Run This Code On RowLoad Event I got below Error :

private void ParentGridView_OnRowLoaded(object sender, EventArgs e)
{
    try
    {
        if(((RadGridView)sender).Columns != null)
        {
            MessageBox.Show(((RadGridView)sender).Columns.Count.ToString(CultureInfo.InvariantCulture));
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Error : Dispatcher processing has been suspended, but messages are still being processed.

Note That the GridView Control is Telerik RadGridView

ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
Amin Eshtiaghi
  • 253
  • 1
  • 3
  • 6

1 Answers1

37

This answer describes the same situation as yours. (It references this answer on a different website).

The dispatcher processing is suspended to avoid reentrancy problems when updating the visual tree.

If you really need to display a message box in response to your "Row Loaded" event, you need to defer the call using `Dispatcher.BeginInvoke().

So, replace:

MessageBox.Show(((RadGridView)sender).Columns.Count.ToString(CultureInfo.InvariantCulture));

with:

var msg = ((RadGridView)sender).Columns.Count.ToString(CultureInfo.InvariantCulture);
Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(msg)));

If this code is in a WPF object, then the Dispatcher property is available. Otherwise, you need to get it from somewhere else.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Dave Tillman
  • 589
  • 4
  • 13
  • Thank you! I was faced with this exception when I using "DragDrop" a Textbox control from window to another window in WPF. – Hieu.Gi Feb 26 '20 at 09:39