Hi everyone,
I am trying to build a code to get the website asynchronously using webclient.downloadstringasync
I want to pass the data thus retrieved from
dowloadstringcompleted
to another function.
This is the code I am using,

dim myString as String
Function GetSite(ByVal as URL as String)
StartDownload(URL)
WebBrowser1.DocumentText = mystring
Retrun Nothing
End Function

Function StartDownload(ByVal URL As String)
Dim _client As New Net.WebClient
        Dim URL As String
        If My.Computer.Network.IsAvailable Then
            _client.DownloadStringAsync(New Uri(URL))
        End If
        AddHandler _client.DownloadStringCompleted, AddressOf DownloadStringCompleted
Return Nothing
End Function

Function DownloadStringCompleted(ByVal sender As Object, _
ByVal e As system.Net.DownloadStringCompletedEventArgs)
               
        If e.Cancelled = False AndAlso e.Error Is Nothing Then
                    myString = CStr(e.Result)
        End If
        Return myString
    End Function

My problem is that I am not being able to pass on the values as StartDownload gets completed, while data is still being retrieved. Any suggestions regarding the solution or work around would be greatly appreciated
Regards

After

AddHandler _client.DownloadStringCompleted, AddressOf DownloadStringCompleted

you should 'wait while is busy'

Do While _client.IsBusy
    System.Windows.Forms.Application.Doevents()
Loop
'
' And wait for the event to be fired
'
System.Windows.Forms.Application.Doevents()

Hope this helps

I'll insert the code and try it, but if I am going to make the thread wait while it is fetching the data doesn't it defeats the whole purpose of accessing the data asynchronously.

@lolafuertes: I tried the code, it doesn't freezes the GUI (sorry for speaking before checking what DoEvents does), however it still returns an empty string. Maybe I need to change my methodology or something.

DoEvents() just softly 'halts' executing the current thread and gives the oportuniti to other threads or applications to acces to CPU time, then returns to the next sentence. So it will loop until IsBusy is false. At this moment, also the DownloadStringCompleted will be fired.

I'm not sure why it returns an empty string as it 'should' return the answer. Should I assume that if you get it sync then you have a non empty response (even the GUI is frozen)?
Did you tried to set the event handler before the call to the DownloadStringAsync?
Is the DownloadStringCompleted event fired at any time?

Thans in advance for the info

Thanks for the replies and information on DoEvents() I am much obliged.
As for the information, yes DownloadString does works fine in "sync" as it returns the required data appropriately.
DownloadStringCompleted is indeed being fired on completion of the DownloadStringAsync, and no it isn't returning a null value rather it is returning a value and right value at it.
What is happening is that Function StartDownload gets completed far much earlier than firing of DownloadStringCompeleted as a result by the time myString gets data from the site (on completion of downloadstringasync), WebBrowser1.DocumentText has already been fired with myString still being Null.
I hope it does makes some sense.
Regards.

Oh and I declaring Event Handler before firing of DownloadStringAsync doesn't help.
Regards.

I Undertand.

Instead of looping on IsBusy you can:

dim myString as String
Function GetSite(ByVal as URL as String)
StartDownload(URL)
WebBrowser1.DocumentText = mystring
Retrun Nothing
End Function
Dim DataReceived as Boolean
Function StartDownload(ByVal URL As String)
Dim _client As New Net.WebClient
        Dim URL As String
        AddHandler _client.DownloadStringCompleted, AddressOf DownloadStringCompleted
        If My.Computer.Network.IsAvailable Then
            DataReceived = False
            _client.DownloadStringAsync(New Uri(URL))
            Do While Not DataReceived
                System.Windows.Forms.Application.Doevents()
            Loop            '
    End If

Return Nothing
End Function

Function DownloadStringCompleted(ByVal sender As Object, _
ByVal e As system.Net.DownloadStringCompletedEventArgs)
    DataReceived = True               
    If e.Cancelled = False AndAlso e.Error Is Nothing Then
         myString = CStr(e.Result)
    End If
Return myString
End Function

Hope this helps

Thanks lolafuertes that works though I tried to use that logic before but sans DoEvents it was freezing the GUI now GUI stays responsive and does what I wanted to achieve. Thanks a lot for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.