Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-01
5
Medium Priority
?
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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