Link to home
Start Free TrialLog in
Avatar of Anubis2005
Anubis2005Flag for Vanuatu

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
Avatar of r1937
r1937

Insert an backgroundworker control and place to the code on DoWork event.
ex:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        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_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        MsgBox(e.Result)
    End Sub
End Class
Avatar of Anubis2005

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.
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.Add("Data" + _Counter.ToString)
            _RemoteRequestDataInner.Add("Data" + (_Counter + 1).ToString)

            ' ADD TO REMOTE REQUEST
            Dim _RemoteRequestNumber As Int32 = _RemoteRequestData.Add(_RemoteRequestDataInner)

            ' EXECUTE BACKGROUND WORK
            _HandleRemoteRequest = New System.ComponentModel.BackgroundWorker()
            _HandleRemoteRequest.RunWorkerAsync(_RemoteRequestData(_RemoteRequestNumber))

        End While

    End Sub


    Private Sub _HandleRemoteRequest_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles _HandleRemoteRequest.DoWork

        ' 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_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles _HandleRemoteRequest.RunWorkerCompleted

        Console.WriteLine("")
        Console.WriteLine("Background Worker Response: " & e.Result)
        Console.WriteLine("")

    End Sub


End Class
ASKER CERTIFIED SOLUTION
Avatar of r1937
r1937

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello r1937,

That was perfect!  

Thank you.
Anubis.