Question Abort/Kill Thread (While Connected to Proxy)

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
Hey everyone, hope I can get some ideas here! I've tried a few different things with this but haven't had much luck thus far....Here's what I'm trying to do.

I've got multiple threads which goes through a list of proxy servers and tests them. Anytime I press the Stop button it seams to abort all of the threads quickly, except once it gets to the last 1 or 2 of them. I'm guessing it's able to finish the requests on most of them rather quickly, but there are 1 or 2 in there which are extremely slow in responding. Is there anyway to just kill these without trying to wait for the response?

I have a timeout set on the webrequest, but that doesn't help at all. It's not often the software sits there for 4 or 5 minutes waiting for the last thread to finish.. Below is some of my code.

ProxyThreadManager.vb
VB.NET:
Option Explicit On
Imports System.Threading

Public Class ProxyThreadManager
    Dim Parent As Form1
    Dim Queue As New List(Of QueueItemProxy)
    Dim Threads As New List(Of ProxyThread)

    Public Sub New(ByVal _parent As Form1)
        Parent = _parent
    End Sub

    Public Sub Start()
        If Queue.Count = 0 Then
            'finished due to empty queue
        Else
            For i As Integer = 1 To IIf(MaximumThreads < (Queue.Count), MaximumThreads, Queue.Count)
                Dim thread As New ProxyThread(Me)
                AddHandler thread.QueueItemCompleted, AddressOf HandleQueueItemCompleted

                Dim t As New Threading.Thread(AddressOf thread.Start)
                t.IsBackground = True
                t.Priority = ThreadPriority.Lowest
                t.SetApartmentState(ApartmentState.STA)
                thread.SystemThread = t

                Threads.Add(thread)
                t.Start()
                Parent.UpdateThreadCount(Threads.Count)
            Next
        End If
    End Sub

    Public Sub Abort()
        For Each t As ProxyThread In Threads
            t.Abort = True
        Next
    End Sub

    Public Sub AddToQueue(ByVal item As QueueItemProxy)
        If item IsNot Nothing Then
            Queue.Add(item)
        End If
    End Sub

    Public Function GetNextQueueItem() As QueueItemProxy
        If Queue.Count = 0 Then
            Return Nothing
        Else
            Dim item As New QueueItemProxy
            item.Url = Queue(0).Url
            Queue.RemoveAt(0)
            Return item
        End If
    End Function

    Public Sub HandleThreadCompleted(ByVal _thread As ProxyThread)
        RemoveHandler _thread.QueueItemCompleted, AddressOf HandleQueueItemCompleted
        Threads.Remove(_thread)
        Parent.UpdateThreadCount(Threads.Count)
        If Threads.Count = 0 Then
            Queue.Clear()
            Parent.HandleCompletedProxy()
        End If
    End Sub

    Private Sub HandleQueueItemCompleted(ByVal item As QueueItemResultProxy)
        Parent.AddProxyServerToList(item.ScrapedProxies)
        Parent.AddToCounterProxy(item.Success)
    End Sub

    Private _maximumThreads As Integer
    Public Property MaximumThreads() As Integer
        Get
            Return _maximumThreads
        End Get
        Set(ByVal value As Integer)
            _maximumThreads = value
        End Set
    End Property
End Class

ProxyThread.vb
VB.NET:
Option Strict On
Imports System.Text
Imports System.Net
Imports System.Text.RegularExpressions
Imports System.Threading

Public Class ProxyThread
    Public Event QueueItemCompleted(ByVal result As QueueItemResultProxy)
    Private parentManager As ProxyThreadManager

    Public Sub New(ByVal _manager As ProxyThreadManager)
        parentManager = _manager
    End Sub

    Public Sub Start()
        Do
            If Abort = True Then parentManager.HandleThreadCompleted(Me) : Exit Sub
            Dim currentItem As QueueItemProxy = parentManager.GetNextQueueItem
            If currentItem Is Nothing = False Then
                Scrape(currentItem.Url)
            Else
                parentManager.HandleThreadCompleted(Me)
                Exit Sub
            End If
        Loop
    End Sub

    Private Sub Scrape(ByVal url As String)
        Dim request As System.Net.WebRequest

        Dim Sw As New Stopwatch
        Dim responseData As String = String.Empty
        Dim sr As System.IO.StreamReader
        Dim response As System.Net.WebResponse
        Dim result As New StringBuilder
        Dim success As Boolean = True

        Try
            request = System.Net.WebRequest.Create("http://www.google.com")
            request.Timeout = 25 * 1000
            request.Proxy = New WebProxy(url.Trim)

            response = request.GetResponse()

            Sw.Start()
            response = CType(request.GetResponse, HttpWebResponse)
            Sw.Stop()

            sr = New System.IO.StreamReader(response.GetResponseStream())
            responseData = sr.ReadToEnd()

            If responseData.Contains("Google") = True Then
                result.AppendLine(url.Trim)  'good
            Else
                success = False 'not good
            End If

            sr.Close()
            response.Close()
        Catch ex As Exception
            success = False
        Finally
            Thread.Sleep(500)
            RaiseEvent QueueItemCompleted(New QueueItemResultProxy(Regex.Replace(result.ToString.Trim, "^\r?\n?$", "", RegexOptions.Multiline), Me, success))
        End Try
    End Sub

    Private _abort As Boolean = False
    Public Property Abort() As Boolean
        Get
            Return _abort
        End Get
        Set(ByVal value As Boolean)
            _abort = value
        End Set
    End Property

    Private _thread As Threading.Thread
    Public Property SystemThread() As Threading.Thread
        Get
            Return _thread
        End Get
        Set(ByVal value As Threading.Thread)
            _thread = value
        End Set
    End Property
End Class

Form1.vb (Parent)
VB.NET:
    Public Sub UpdateThreadCount(ByVal count As Integer)
        If Me.InvokeRequired = True Then
            Me.Invoke(New UpdateThreadCountDelegate(AddressOf UpdateThreadCount), count)
        Else
            lblActiveThreads.Text = count
        End If
    End Sub

    Public Sub AddToCounterProxy(ByVal success As Boolean)
        If Me.InvokeRequired = True Then
            Me.Invoke(New ProxyAddToCounterDelegate(AddressOf AddToCounterProxy), success)
        Else
            ProgressBar1.PerformStep()
        End If
    End Sub

    Public Sub AddProxyServerToList(ByVal proxy As String)
        If String.IsNullOrEmpty(proxy) Then Exit Sub
        If Me.InvokeRequired = True Then
            Me.Invoke(New ProxyAddProxiesToListDelegate(AddressOf AddProxyServerToList), proxy)
        Else
            ValidProxies.Add(proxy)  'Add valid proxy server
            lblTotalValidProxies.Text = lblTotalValidProxies.Text + 1
        End If
    End Sub

    Public Sub HandleCompletedProxy()
        If Me.InvokeRequired = True Then
            Me.Invoke(New ProxyHandleCompletedDelegate(AddressOf HandleCompletedProxy))
        Else
            If StartToolStripMenuItem.Enabled = False Then
                reset()
                Exit Sub
            Else
                msgbox("Finished!")
            End If
        End If
    End Sub

Thanks in advance!
 
Top Bottom