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.

,

  1. Hinterlasse einen Kommentar

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit deinem WordPress.com-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s

%d Bloggern gefällt das: