9

Is there a way to do idle time processing in WPF application equivalent to OnIdle event in MFC?

Ashish Ashu
  • 14,169
  • 37
  • 86
  • 117

3 Answers3

11

You can dispatch a task (using the Dispatcher in the normal way) with a DispatcherPriority of ApplicationIdle, which will only be executed when the application is idle. Sample code:

DispatcherPriority priority = DispatcherPriority.ApplicationIdle;    
Application.Current.Dispatcher.BeginInvoke(priority, action);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I have put the code in the constructor and noticed that the task will is executed immediately when we double click the application. I am wondering where do I put my code that will execute whenever my application is not recieving any event or Message q is empty. – Ashish Ashu Oct 07 '10 at 03:17
  • If the code is in the constructor you will want to dispatch the creation of the object (as above with ApplicationIdle priority) as the constructor will always run immediately on creation! Dispatcher.BeginInvoke(new Action(CreateMyObject), DispatcherPriority.ApplicationIdle); Here CreateMyObject is a method that creates your object and Action is the delegate - it can include parameters, see: http://msdn.microsoft.com/en-us/library/018hxwa8.aspx – markmnl Oct 07 '10 at 03:40
6

It is the Dispatcher.Hooks.DispatcherInactive event.

Oleg Sych
  • 6,548
  • 36
  • 34
1

A late alternative answer (as a memo to myself):

System.Windows.Interop.ComponentDispatcher.ThreadIdle += (_, __) =>
{
    Debug.Print("Idle");
};
noseratio
  • 59,932
  • 34
  • 208
  • 486