IdentifiantMot de passe
Loading...
Mot de passe oubli� ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les r�ponses en temps r�el, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

instruction await pour methodes asynchrones


Sujet :

C#

  1. #1
    Membre �clair�
    Profil pro
    Inscrit en
    F�vrier 2007
    Messages
    915
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : F�vrier 2007
    Messages : 915
    Par d�faut instruction await pour methodes asynchrones
    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

  2. #2
    Membre Expert
    Avatar de PixelJuice
    Homme Profil pro
    Ing�nieur .NET & Game Designer
    Inscrit en
    Janvier 2014
    Messages
    667
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activit� : Ing�nieur .NET & Game Designer
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Janvier 2014
    Messages : 667
    Par d�faut
    Bonjour,

    C'est tr�s simple :

    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    Task<int> downloading = DownloadDocsMainPageAsync();
    La m�thode est lanc�e en mode "on s'en fou", et donc elle fait sa vie et tu ne l'attends pas, il faut donc mettre un await l� aussi (A noter que ce n'est pas une mauvaise chose en soi, mais ce n'est pas le but recherch� ici)


    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    int downloading = await DownloadDocsMainPageAsync();
    Tu peux voir d'ailleurs que la variable de retour fait beaucoup plus sens d'un coup.

  3. #3
    Membre �clair�
    Profil pro
    Inscrit en
    F�vrier 2007
    Messages
    915
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : F�vrier 2007
    Messages : 915
    Par d�faut
    Merci pour la r�ponse, apr�s du temps pass� dessus j'ai trouv� une solution en testant le status de la tache :


    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
     
    int status=(int)downloading.Status;
        if (status==1)
    {
        downloading.Wait();
    }
    Ca fonctionne mais je n'ai pas compris comme tu fais j'ai essay� avec ta fa�on et sauf erreur de ma part ca ne marche pas. Mon 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
     
        public static async Task Main()
        {
            Task<int> downloading = DownloadDocsMainPageAsync();
            Console.WriteLine($"{nameof(Main)}: Launched downloading.");
            int status=(int)downloading.Status;
                if (status==1)
            {
                downloading.Wait();
            }
            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.");
            Console.WriteLine(content.Length);
            return content.Length;
        }
    }
    Merci si tu veux revenir sur le sujet n'h�site pas merci

  4. #4
    Membre �clair�
    Profil pro
    Inscrit en
    F�vrier 2007
    Messages
    915
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : F�vrier 2007
    Messages : 915
    Par d�faut Modif
    Finalement pas besoin de tester le status suffit

+ R�pondre � la discussion
Cette discussion est r�solue.

Discussions similaires

  1. R�ponses: 24
    Dernier message: 13/11/2006, 09h00
  2. instruction valable pour une ligne d'une table
    Par Skizo dans le forum Access
    R�ponses: 2
    Dernier message: 18/05/2006, 15h51
  3. [SOAP] API pour appels asynchrones
    Par Dar Shak dans le forum API standards et tierces
    R�ponses: 3
    Dernier message: 26/04/2005, 08h57
  4. [C#] [Sockets] Eclaircissements pour connexions asynchrones
    Par Sinclair dans le forum Windows Forms
    R�ponses: 6
    Dernier message: 08/06/2004, 17h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo