Bjr à tous, j'essaie de comprendre le fonctionnement des méthodes asynchrones en utilisant un exemple vu sur la doc de microsoft :

https://learn.microsoft.com/fr-fr/do...perators/await

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
 
namespace TestsSharpPcap
{
    public class AwaitOperator
    {
        public static async Task Main()
        {
            Task<int> downloading = DownloadDocsMainPageAsync();
            Console.WriteLine($"{nameof(Main)}: Launched downloading.");
 
            int bytesLoaded = await downloading;
            Console.WriteLine($"{nameof(Main)}: Downloaded {bytesLoaded} bytes.");
        }
 
        private static async Task<int> DownloadDocsMainPageAsync()
        {
            Console.WriteLine($"{nameof(DownloadDocsMainPageAsync)}: About to start downloading.");
            var client = new HttpClient();
            byte[] content = await client.GetByteArrayAsync("http://www.google.com/");
            Console.WriteLine($"{nameof(DownloadDocsMainPageAsync)}: Finished downloading.");
            return content.Length;
        }
    }
}
Le problème que j'ai est que quand la tache "DownloadDocsMainPageAsync" est lancée et qu'elle arrive à l'opérateur await elle s'exécute et termine le programme sans exécuter le reste du code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
 
Console.WriteLine($"{nameof(DownloadDocsMainPageAsync)}: Finished downloading.");
return content.Length;
ce qui donne comme résultat :

DownloadDocsMainPageAsync: About to start downloading.
Main: Launched downloading.

au lieu comme le stipule le cours :

// Output similar to:
// DownloadDocsMainPageAsync: About to start downloading.
// Main: Launched downloading.
// DownloadDocsMainPageAsync: Finished downloading.
// Main: Downloaded 27700 bytes.

Ils précisent : "L’opérande d’une expression await doit fournir une notification lorsqu’une tâche se termine. En général, un délégué est appelé lorsque la tâche se termine, que ce soit une réussite ou un échec. La section await de la spécification du langage C# fournit les détails sur la façon dont ces notifications sont implémentées."
mais je n'arrive pas à voir d'exemples précis, si quelqu'un a une idée MERCI