Beiträge getaggt mit WPF

Application.OnIdle equivalent for WPF

If you need an alternativ to the WinForms Application.OnIdle event in WPF you can use the DispatcherTimer.

DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.ApplicationIdle);
timer.Tick += (s, e) => {
    // Do something
};
timer.Start();

Do you only need the function executet once on idle you use BeginInvoke with Priority Idle.

Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => {
    // Do something
}), DispatcherPriority.ApplicationIdle);

If you don’t like the lambda-style you can also write this:

Dispatcher.CurrentDispatcher.BeginInvoke((Action)doSomething, DispatcherPriority.ApplicationIdle);

Here doSomething needs to be a void function without parameters.

Advertisements

,

Hinterlasse einen Kommentar