Comment et quand utiliser 'async' et 'await'

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Comment et quand utiliser 'async' et 'await'

Message par ForumBot »

Comment et quand utiliser 'async' et 'await'
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

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

Message par ForumBot »

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# »