multi-threaded proxy checker having problems

Posted by Paul on Stack Overflow See other posts from Stack Overflow or by Paul
Published on 2011-02-01T19:44:23Z Indexed on 2011/02/01 23:25 UTC
Read the original article Hit count: 318

hello everyone, I am trying to create a proxy checker. This is my first attempt at multithreading and it's not going so well, the threads seem to be waiting for one to complete before initializing the next.

Imports System.Net
Imports System.IO
Imports System.Threading

Public Class Form1
    Public sFileName As String
    Public srFileReader As System.IO.StreamReader
    Public sInputLine As String

    Public Class WebCall
        Public proxy As String
        Public htmlout As String

        Public Sub New(ByVal proxy As String)
            Me.proxy = proxy
        End Sub

        Public Event ThreadComplete(ByVal htmlout As String)

        Public Sub send()
            Dim myWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.myserver.com/ip.php"), HttpWebRequest)
            myWebRequest.Proxy = New WebProxy(proxy, False)
            Try
                Dim myWebResponse As HttpWebResponse = CType(myWebRequest.GetResponse(), HttpWebResponse)
                Dim loResponseStream As StreamReader = New StreamReader(myWebResponse.GetResponseStream())
                htmlout = loResponseStream.ReadToEnd()
                Debug.WriteLine("Finished - " & htmlout)
                RaiseEvent ThreadComplete(htmlout)
            Catch ex As WebException
                If (ex.Status = WebExceptionStatus.ConnectFailure) Then
                End If
                Debug.WriteLine("Failed - " & proxy)
            End Try
        End Sub
    End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim proxy As String
        Dim webArray As New ArrayList()
        Dim n As Integer
        For n = 0 To 2
            proxy = srFileReader.ReadLine()
            webArray.Add(New WebCall(proxy))
        Next

        Dim w As WebCall
        For Each w In webArray
            Threading.ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf w.send), w)
        Next w

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        srFileReader = System.IO.File.OpenText("proxies.txt")
    End Sub
End Class

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about multithreading