Performs the specified action after the specified number of milliseconds.
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
public static void DoAfterDelay(int millisecondsDelay, Action action)
{
var withoutAwait = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
Windows.UI.Core.CoreDispatcherPriority.Normal,
async () => { await Task.Delay(millisecondsDelay); action(); });
}Although DoAfterDelay uses async in its implementation, it is not an async method. This is because callers do not need to await any results. Therefore, it does not await its RunAsync call and does not return a Task. (The withoutAwait variable is present only to suppress the warning about not using "await".) DoAfterDelay could be an "async void" method, but this is not recommended except with event handlers because callers expect to be able to await async methods.
One scenario where this method is useful is to hide a temporary message after displaying it briefly.
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
// Normally declared in XAML.
TextBlock AlertMessage { get; } = new TextBlock { Text = "Alert!" };
public void ShowAlertForFiveSeconds()
{
AlertMessage.Visibility = Visibility.Visible;
DoAfterDelay(5000, () => AlertMessage.Visibility = Visibility.Collapsed);
}CoreDispatcher.RunAsync method
Task.Delay method
Lambda expressions (anonymous methods using the "=>" syntax)
This method is used by the RSS Reader sample to hide a temporary message.