Solved

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

Posted on 2007-04-01
5
223 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

21 Experts available now in Live!

Get 1:1 Help Now