views:

61

answers:

2

I have a wpf application that populates an Infragistics XamDataGrid by the usual method of binding an observable collection to the grid. As each data item is populated into the collection, one at a time, the grid updates. I also have a cancel button that I would like to immediately stop the population if the user clicks it. However, it takes a few seconds or more to respond to the cancel.

The problem (I think) is that the message loop is full of the grid population events and my cancel is way in the back and must wait its turn. I was wondering if there is a way to insert a message in the front of the queue and thus make the cancel more responsive (hacky or not - if hacky, please explain what ill effects I can expect).

I am not experiencing bad performance; in fact the UI is quite responsive. The problem is purely that the cancel event has to wait its turn in the message queue and I would rather it have priority over the population messages.

edit: clarifications

+1  A: 

When you say Grid, what kind of Grid do you mean? I think your issue might be that for a large collection, this setup might not use any type of item virtualization. You might be better off using a ListBox or ListView in this case, which can use a VirtualizingStackPanel to generate UIElements for only the on-screen items.

If this is the case, your ui thread is getting bogged down because its generating elements for every new item, whether or not it's being displayed on screen. If you're using a 3rd party Grid, you might check to see if it has any virtualization functionality built in that you can turn on.

Mark Synowiec
Sorry, I should have specified: an Infragistics XamDatagrid. Thanks for the pointer about virtualization - I'll check into it and accept the answer if it turns out to be what I need in this case.
Jason
It turns out that I seem to already have virtualization turned on. That does help with performance but doesn't solve the original problem, which is that the cancel button windows message has to wait its turn to be processed rather than getting priority.
Jason
+1  A: 

The answer ended up being the DispatcherPriority:

 private void btnCancel_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Dispatcher.Invoke(new Action(() => btnCancel.Command.Execute(null)), DispatcherPriority.Send);
        }

"Send" priority is the highest of an enumerated type, with "SystemIdle" being the lowest. When I invoke the button's command with that priority, the Cancel goes through immediately.

Jason