WPF High Frequency Events and Updating the UI

Viewed 306

I have a WPF application that is connected to a USB device. Said device raises an event at a rate of about 10/second. The data from these events updates a bar chart and a line chart. The bar chart represents the data from each event as they occur. The line chart is used to show values over time and is read from a collection of historical values from the start of the measurement.

My issue is, after about 3-5 minutes of measurements, the UI becomes very slow and eventually unresponsive. I think it is due to the below code snippet occurring too often and eventually locking up the UI thread.

System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => 
{ 
    Update the collection here.
}));

Is there a better way to update the collection so as to not lock up the UI?

1 Answers

Surely you have already solved your problem. However, it is not a memory leak.
Most likely BeginInvokes enqueues more "update collection"-tasks than can be worked off by the Dispatcher. At some point the Dispatcher has to process thousands of tasks and at the same time BeginInvoke enqueues 10 new tasks every second. To solve the problem, you need to make sure that the Dispatcher is not overwhelmed with new tasks. You could update the UI from a separate thread with the latest data only.

Related