dokumentacji MSDN jest rozczarowujące, ale decompiling Task.Delay
przy użyciu reflektor daje Więcej informacji:
public static Task Delay(int millisecondsDelay, CancellationToken cancellationToken)
{
if (millisecondsDelay < -1)
{
throw new ArgumentOutOfRangeException("millisecondsDelay", Environment.GetResourceString("Task_Delay_InvalidMillisecondsDelay"));
}
if (cancellationToken.IsCancellationRequested)
{
return FromCancellation(cancellationToken);
}
if (millisecondsDelay == 0)
{
return CompletedTask;
}
DelayPromise state = new DelayPromise(cancellationToken);
if (cancellationToken.CanBeCanceled)
{
state.Registration = cancellationToken.InternalRegisterWithoutEC(delegate (object state) {
((DelayPromise) state).Complete();
}, state);
}
if (millisecondsDelay != -1)
{
state.Timer = new Timer(delegate (object state) {
((DelayPromise) state).Complete();
}, state, millisecondsDelay, -1);
state.Timer.KeepRootedWhileScheduled();
}
return state;
}
Zasadniczo metoda ta jest po prostu timer zawinięty wewnątrz zadania. Więc tak, możesz powiedzieć, że to tak jak timer.
Tutaj jest trochę [wiki] (http://social.technet.microsoft.com/wiki/contents/articles/21177.visual-c-thread-sleep-vs-task-delay.aspx) z wyróżnieniem demo niektóre różnice w blokowaniu między tymi dwoma. – jxramos
Możliwy duplikat [Thread.Sleep (2500) vs. Task.Delay (2500) .Wait()] (http://stackoverflow.com/questions/34052381/thread-sleep2500-vs-task-delay2500-wait) –