Я прочитал несколько документов, и я узнал, что await Task.Delay(1000)
не блокирует поток.
Но в этом примере кода кажется, что он блокирует потоки:
var task = new Task(async () => {
Console.WriteLine(" ====== begin Delay()");
for (int i = 1; i < 5; i++)
{
Console.WriteLine(" ===Delay=== " + i);
Console.WriteLine("the task thread id: " + Thread.CurrentThread.ManagedThreadId + "; the task id is: " + Task.CurrentId);
await Task.Delay(1000);
Console.WriteLine("**ddd***:"+i);
}
Console.WriteLine(" ====== end Delay()");
});
task.Start();
он печатает:
====== begin Delay()
===Delay=== 1
the task thread id: 3; the task id is: 1
**ddd***:1
===Delay=== 2
the task thread id: 4; the task id is:
**ddd***:2
===Delay=== 3
the task thread id: 3; the task id is:
**ddd***:3
===Delay=== 4
the task thread id: 4; the task id is:
**ddd***:4
====== end Delay()
в соответствии с распечаткой, он выполняет код в формате c.
Я думал, что это напечатает что-то вроде следующего:
====== begin Delay()
===Delay=== 1
the task thread id: 3; the task id is: 1
===Delay=== 2
the task thread id: 4; the task id is:
===Delay=== 3
the task thread id: 3; the task id is:
===Delay=== 4
the task thread id: 4; the task id is:
**ddd***:1
**ddd***:2
**ddd***:3
**ddd***:4
====== end Delay()
Так что я запутался, может кто-нибудь объяснить, пожалуйста, поведение? Спасибо.