Comment recevoir un retour local avec DownloadStringAsync ?

Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 - 10 déc. 2010 à 12:28
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 - 10 déc. 2010 à 13:58
Bonjour,

dans l'exemple ci dessous, je souhaite remplacer myWeb.DownloadString(myURI) qui gèle le thread par myWeb.DownloadStringAsync(myURI).

Mon soucis est que DownloadStringAsync ne renvoi pas de buffer string !

Serai t'il possible de récupérer dans TestUrl() un retour du buffer malgré le AddressOf ET sans avoir recours à une variable public ?
Exemple top: AddressOf RtBuffer=DownloadOk

Sub TestUrl()
     Dim myURI As New System.Uri("http://www.google.fr")

     myWeb.DownloadStringAsync(myURI)
     AddHandler myWeb.DownloadStringCompleted, AddressOf DownloadOk
     ' code magique pour récupérer un retour local de DownloadOk
End Sub

Function DownloadOk()
     Dim myString As String = ""
     If e.Cancelled = False AndAlso e.Error Is Nothing Then
          myString = e.Result.ToString
     Else
          myString = ""
     End If
     Return myString
End Function 

1 réponse

Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
10 déc. 2010 à 13:58
Solution de remplacement :(

Une variable public aux fonctions et une boucle.

Shared Buffer As String = ""

Sub TestUrl()
     Dim myURI As New System.Uri("http://www.google.fr")

     myWeb.DownloadStringAsync(myURI)
     AddHandler myWeb.DownloadStringCompleted, AddressOf DownloadOk
     'code pas magique pour récupérer un retour public de DownloadOk
     While Buffer = ""
          Application.DoEvents()
     End While
End Sub

Sub DownloadOk(ByVal sender As Object, ByVal e As System.Net.DownloadStringCompletedEventArgs)
     If e.Result = "" Then
          Buffer = "NOP"
     Else
          Buffer = e.Result
     End If
End Function 
0
Rejoignez-nous