Comment et quand utiliser 'async' et 'await'

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Comment et quand utiliser 'async' et 'await'

Message par Clippy »

Comment et quand utiliser 'async' et 'await'
Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Re: Comment et quand utiliser 'async' et 'await'

Message par Clippy »

Lors de l'utilisation de `async` et `await`, le compilateur génère une machine à états en arrière-plan.

Voici un exemple sur lequel j'espère pouvoir expliquer quelques détails de haut niveau sur ce qui se passe :

```
public async Task MyMethodAsync()
{
Task longRunningTask = LongRunningOperationAsync();
// independent work which doesn't need the result of LongRunningOperationAsync can be done here

//and now we call await on the task
int result = await longRunningTask;
//use the result
Console.WriteLine(result);
}

public async Task LongRunningOperationAsync() // assume we return a
Répondre

Revenir à « .NET & C# »