문제

Assuming I have this code:

public void AAA()
{    
    BBB(); 
}  

public async Task BBB() 
{ 
    await Task.Delay(10000); 
}  

IMHO, in normal mode, the method AAA should also be async so that the main thread will continue its job without waiting.

But I have 2 question regarding this situation here:

  1. Is it correct to say that the code above does tie a thread for 10 sec? (I suspect that the answer is yes because AAA is not async so there is no registration for continuation)

  2. If so - how can I prove it via code?

도움이 되었습니까?

해결책

Is it correct to say that the code above does tie a thread for 10 sec?

No.

AAA calls BBB, which sets up a Task and returns immediately. Since AAA does not await on BBB, it also returns immediately. So "your own" thread is not tied up.

The "implementation" of Task.Delay itself makes use of a timer instead of blocking a thread, so neither is any thread pool thread tied up for the duration of the task.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top