Solved

How to get WebClient() call to execute in its own thread?

Posted on 2007-04-01
5
226 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:Anubis2005
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:r1937
ID: 18835618
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
0
 
LVL 1

Author Comment

by:Anubis2005
ID: 18846569
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.
0
 
LVL 1

Author Comment

by:Anubis2005
ID: 18846977
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
0
 
LVL 3

Accepted Solution

by:
r1937 earned 500 total points
ID: 18859511
OK I think I got something:
-----------------------------------------------

Imports System.ComponentModel

Public Class ExampleClass

    Private _RemoteRequestData As ArrayList = New ArrayList
    '===>>>Remove WithEvents
    '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
            '===>>>Small Change Here
            Dim _HandleRemoteRequest As New BackgroundWorker()
            AddHandler _HandleRemoteRequest.DoWork, AddressOf _HandleRemoteRequest_DoWork
            AddHandler _HandleRemoteRequest.RunWorkerCompleted, AddressOf _HandleRemoteRequest_RunWorkerCompleted
            _HandleRemoteRequest.RunWorkerAsync(_RemoteRequestData(_RemoteRequestNumber))

        End While

    End Sub

    '===>>>Remove Handle part
    'Private Sub _HandleRemoteRequest_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles _HandleRemoteRequest.DoWork
    Private Sub _HandleRemoteRequest_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)

        ' 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

    '===>>>Remove Handle part
    'Private Sub _HandleRemoteRequest_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles _HandleRemoteRequest.RunWorkerCompleted
    Private Sub _HandleRemoteRequest_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)

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

    End Sub

End Class
0
 
LVL 1

Author Comment

by:Anubis2005
ID: 18860943
Hello r1937,

That was perfect!  

Thank you.
Anubis.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now