Pregunta

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?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top