列表中的VB.net线程不会中止

时间:2017-10-15 17:24:35

标签: vb.net multithreading abort

所以我试图检查256个线程的代理列表。我决定创建新线程并将它们存储在线程列表中。然后,当有人想要停止检查。我做一个for循环并中止每个线程。问题是......这不起作用,它只是继续但冻结应用程序。我尝试将线程作为后台线程,但这也无济于事。

Private Sub AbortChkerBtn_Click(sender As Object, e As EventArgs) Handles AbortChkerBtn.Click

    ' MessageBox.Show("test")
    For Each Checker In CheckerList

        Checker.Abort()
        ActiveThreads -= 1

    Next

    Text = "ProxyDeluxe!"

    ChkProxiesBtn.Enabled = True
    AbortChkerBtn.Enabled = False

    If WorkingProxies.Count > 0 Then

        ClearChkedBtn.Enabled = True
        SaveChckedBtn.Enabled = True

    End If


End Sub

Private Sub CheckProxies(ByVal Proxies As List(Of String))

    ActiveThreads += 1

    For Each Proxy In Proxies

        Try
            Dim myProxy As New WebProxy(Proxy)
            Dim r As HttpWebRequest = WebRequest.Create("https://www.google.com/")
            r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, Like Gecko) Chrome/29.0.1547.2 Safari/537.36"
            r.Timeout = 5000
            r.Proxy = myProxy
            Dim re As HttpWebResponse = r.GetResponse()
            re = r.GetResponse()
            Working += 1
            WorkingProxies.Add(Proxy)
        Catch ex As Exception

        End Try

        WorkingProxiesLbl.Text = "Working Proxies: " & Working

    Next

    ActiveThreads -= 1

End Sub

Private Sub CreateThreads()

    Dim AmountOfThreads As Integer = Math.Min(ProxyList.Count, 256)

    For i As Integer = 0 To AmountOfThreads

        Dim a As Double = ProxyList.Count / AmountOfThreads * i
        Dim list2 As List(Of String) = ProxyList.GetRange(Math.Floor(a), Math.Min(ProxyList.Count, a + ProxyList.Count / AmountOfThreads) - a)
        Dim ProxyChecker As New Thread(AddressOf CheckProxies)
        CheckerList.Add(ProxyChecker)
        ProxyChecker.Start(list2)

    Next

    While ActiveThreads > 0

        Text = "ActiveThreads: " & ActiveThreads

    End While

End Sub

Private Sub ChkProxiesBtn_Click(sender As Object, e As EventArgs) Handles ChkProxiesBtn.Click
    WorkingProxies.Clear()
    CheckedListbox.Items.Clear()
    Dim ThreadCreator As New Thread(AddressOf CreateThreads)
    ThreadCreator.Start()
    ChkProxiesBtn.Enabled = False
    AbortChkerBtn.Enabled = True
End Sub

Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing

    If ProxyGrabber.IsAlive Then

        ProxyGrabber.Abort()

    End If

    For Each Thread In CheckerList

        If Thread.IsAlive Then

            Thread.Abort()

        End If

    Next

End Sub

End Class

0 个答案:

没有答案