Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 93
Filtering a datagrid view 8 42
VB.Net  Exporting Excel Woorkbook to PDF 7 69
vb.net background worker crossthread 11 42
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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