Anubis2005
asked on
How to get WebClient() call to execute in its own thread?
Hello All,
I'm still very new to VB.NET and I would like to know the code required to allow this request to be able to be called and executed in its own thread so as not to stop the script that called it.
Dim wc As New System.Net.WebClient()
Dim myResponse As String = wc.DownloadString("http://localhost/PostTest.php")
Thanks.
Anubis
I'm still very new to VB.NET and I would like to know the code required to allow this request to be able to be called and executed in its own thread so as not to stop the script that called it.
Dim wc As New System.Net.WebClient()
Dim myResponse As String = wc.DownloadString("http://localhost/PostTest.php")
Thanks.
Anubis
ASKER
Hello,
Thanks for the prompt reply. I apologize if I'm missing something, I'm still very new to vb.net or .net in all, and I'm getting the following errors:
Name 'BackgroundWorker1' is not declared.
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
Thanks
Anubis.
Thanks for the prompt reply. I apologize if I'm missing something, I'm still very new to vb.net or .net in all, and I'm getting the following errors:
Name 'BackgroundWorker1' is not declared.
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
Thanks
Anubis.
ASKER
Hello,
Apologies, I have now got it to work, but it has now raised another problem. In the sub which is creating the background theads it needs to create one or more threads executing the same task with different parameters. The problem is that once a thread is created, it's overridden by the next and subsequentally only the last created thread is executed. How can I get it to create them as individual threads so it won't override its self?
Below is a snippet of the code being used.
Thanks
Anubis.
Imports System.ComponentModel
Public Class ExampleClass
Private _RemoteRequestData As ArrayList = New ArrayList
Private WithEvents _HandleRemoteRequest As BackgroundWorker
Public Sub HandleAction()
Dim _Counter As Int32 = 0
' THE LOOP WHICH GENERATES THE RESPONSE
While (_Counter < 2)
' DEFINE DATA FOR REMOTE REQUEST
_Counter += 1
Dim _RemoteRequestDataInner As ArrayList = New ArrayList
_RemoteRequestDataInner.Ad d("Data" + _Counter.ToString)
_RemoteRequestDataInner.Ad d("Data" + (_Counter + 1).ToString)
' ADD TO REMOTE REQUEST
Dim _RemoteRequestNumber As Int32 = _RemoteRequestData.Add(_Re moteReques tDataInner )
' EXECUTE BACKGROUND WORK
_HandleRemoteRequest = New System.ComponentModel.Back groundWork er()
_HandleRemoteRequest.RunWo rkerAsync( _RemoteReq uestData(_ RemoteRequ estNumber) )
End While
End Sub
Private Sub _HandleRemoteRequest_DoWor k(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles _HandleRemoteRequest.DoWor k
' ACCESS THE WORKER THAT CREATED THIS REQUEST
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
' GET THE EVENT DATA
Dim _RemoteRequestData As ArrayList = e.Argument
'PROCESS WORK WITH DATA
Console.WriteLine("WORK... DONE")
e.Result = "OK"
End Sub
Private Sub _HandleRemoteRequest_RunWo rkerComple ted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArg s) Handles _HandleRemoteRequest.RunWo rkerComple ted
Console.WriteLine("")
Console.WriteLine("Backgro und Worker Response: " & e.Result)
Console.WriteLine("")
End Sub
End Class
Apologies, I have now got it to work, but it has now raised another problem. In the sub which is creating the background theads it needs to create one or more threads executing the same task with different parameters. The problem is that once a thread is created, it's overridden by the next and subsequentally only the last created thread is executed. How can I get it to create them as individual threads so it won't override its self?
Below is a snippet of the code being used.
Thanks
Anubis.
Imports System.ComponentModel
Public Class ExampleClass
Private _RemoteRequestData As ArrayList = New ArrayList
Private WithEvents _HandleRemoteRequest As BackgroundWorker
Public Sub HandleAction()
Dim _Counter As Int32 = 0
' THE LOOP WHICH GENERATES THE RESPONSE
While (_Counter < 2)
' DEFINE DATA FOR REMOTE REQUEST
_Counter += 1
Dim _RemoteRequestDataInner As ArrayList = New ArrayList
_RemoteRequestDataInner.Ad
_RemoteRequestDataInner.Ad
' ADD TO REMOTE REQUEST
Dim _RemoteRequestNumber As Int32 = _RemoteRequestData.Add(_Re
' EXECUTE BACKGROUND WORK
_HandleRemoteRequest = New System.ComponentModel.Back
_HandleRemoteRequest.RunWo
End While
End Sub
Private Sub _HandleRemoteRequest_DoWor
' ACCESS THE WORKER THAT CREATED THIS REQUEST
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
' GET THE EVENT DATA
Dim _RemoteRequestData As ArrayList = e.Argument
'PROCESS WORK WITH DATA
Console.WriteLine("WORK...
e.Result = "OK"
End Sub
Private Sub _HandleRemoteRequest_RunWo
Console.WriteLine("")
Console.WriteLine("Backgro
Console.WriteLine("")
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello r1937,
That was perfect!
Thank you.
Anubis.
That was perfect!
Thank you.
Anubis.
ex:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BackgroundWorker1.RunWorke
End Sub
Private Sub BackgroundWorker1_DoWork(B
Dim wc As New System.Net.WebClient()
Dim myResponse As String = wc.DownloadString("http://localhost/PostTest.php")
e.Result = myResponse
End Sub
Private Sub BackgroundWorker1_RunWorke
MsgBox(e.Result)
End Sub
End Class