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
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
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; } } }
ce qui donne comme r�sultat :
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3 Console.WriteLine($"{nameof(DownloadDocsMainPageAsync)}: Finished downloading."); return content.Length;
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
Partager