Is there a way to do idle time processing in WPF application equivalent to OnIdle event in MFC?
Asked
Active
Viewed 5,912 times
3 Answers
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
1
A late alternative answer (as a memo to myself):
System.Windows.Interop.ComponentDispatcher.ThreadIdle += (_, __) =>
{
Debug.Print("Idle");
};

noseratio
- 59,932
- 34
- 208
- 486