The OnIdle Event is fired as soon as the computer does not have any tasks to execute. Thus it is a very convenient way to decide when refreshing graphical components. Assuming you have a program that streams a bunch of data from a given source, each time a couple of data arrive you trigger the refresh display function along with your processing functions. What will appends is that the graphical elements refreshing is pretty slow, thus the data start to accumulate in the buffer and the time lag between the data live stream and the graphical result on the screen increases.
The solution to this issue is to process the data when they arrive, but not do any refreshing on the screen nor the graphical components. The screen will be refreshed only when the computer has time, which will be the case when the event OnIdle fires.
Use the following code to subscribe to the OnIdle Event:
using System.Windows.Interop;
private void
Window_Loaded(object sender, RoutedEventArgs e)
{
ComponentDispatcher.ThreadIdle += new EventHandler(OnIdle);
}
The method where to put the refreshing functions:
private void OnIdle(object sender, EventArgs
e)
{
// Refreshing display
functions
}
Notice that this way of processing the data and refreshing the screen, prioritize the processing (possible also the recording) of the data compared to displaying the data to the user. The human eye can only see 25 images per second anyway, so it does not make sense in most of the case to refresh each time a data arrives.