(Dispatcher), :
void MyNonAsyncFunction()
{
Dispatcher.InvokeAsync(async () =>
{
await Task.Delay(1000);
MessageBox.Show("Thank you for waiting");
});
}
, . , , , , :
async void MyAsyncFunction()
{
await Task.Delay(1000);
MessageBox.Show("Thank you for waiting");
}
, .
Since you may not have a dispatcher or you want to use it, but still want to schedule several operations at different times, I would use a thread:
static void MyFunction()
{
Schedule(1000, delegate
{
System.Diagnostics.Debug.WriteLine("Thanks for waiting");
});
}
static void Schedule(int delayMs, Action action)
{
#if DONT_USE_THREADPOOL
new System.Threading.Thread(async () =>
{
await Task.Delay(delayMs);
action();
}
).Start();
#else
Task.Run(async delegate
{
await Task.Delay(delayMs);
action();
});
#endif
}
If you want to avoid async, I would recommend not using threadpool and replacing the call with a Task.Delay(delayMs)callThread.Sleep(delayMs)
source
share