Hi friends, here is some example code that you can refer to if you can help...
Ok, in Form1's Load method, I am creating a list of Account objects, and iterating over the list with a For Each loop 2 times.
The first time, I am calling each accounts Login method, which uses the synchronous method DownloadString from the Net.Webclient class. Since it is synchronous, once all accounts logged in, a message box will display saying the work is complete.
This is fine if there are only a few accounts, but if the list is large, I want to login to all the accounts asynchronously. I believe the correct way to do this is to use the WebClients DownloadStringAsync method. This works good, but as you can see in the Form1 Load method, the 2nd message box is being shown saying the work is complete before it really is. What is the best method to keep track of the progress of the 2nd For Each loop? Do I need to a integer field which is incremented everytime the WebClient in LoginAsync's DownloadStringCompleted even is fired?
Thanks, even though I have a code example and it's kind of long winded, I think my question is pretty simple.
Code:
Public Class Form1
Private accountList As List(Of Account)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
For Each acct As Account In accountList
acct.Login()
Next
MsgBox("Done logging into accounts synchronously.")
For Each acct As Account In accountList
acct.LoginAsync()
Next
MsgBox("Done logging into accounts asynchronously.")
End Sub
End Class
Public Class Account
Property Username As String
Property Password As String
Property IsLoggedIn As Boolean
Public Sub Login()
Using wc As New Net.WebClient
Dim page As String = wc.DownloadString("http://example.com/login.php?username=" & Username & "&password=" & Password)
Me.IsLoggedIn = True
End Using
End Sub
Public Sub LoginAsync()
Using wc As New Net.WebClient
AddHandler wc.DownloadStringCompleted, Sub(s, ev)
Me.IsLoggedIn = True
End Sub
wc.DownloadStringAsync(New Uri("http://example.com/login.php?username=" & Username & "&password=" & Password))
End Using
End Sub
End Class
The first time, I am calling each accounts Login method, which uses the synchronous method DownloadString from the Net.Webclient class. Since it is synchronous, once all accounts logged in, a message box will display saying the work is complete.
This is fine if there are only a few accounts, but if the list is large, I want to login to all the accounts asynchronously. I believe the correct way to do this is to use the WebClients DownloadStringAsync method. This works good, but as you can see in the Form1 Load method, the 2nd message box is being shown saying the work is complete before it really is. What is the best method to keep track of the progress of the 2nd For Each loop? Do I need to a integer field which is incremented everytime the WebClient in LoginAsync's DownloadStringCompleted even is fired?
Thanks, even though I have a code example and it's kind of long winded, I think my question is pretty simple.