多线程webrequests?

时间:2014-10-24 06:54:23

标签: vb.net

我编写了一个工具,可以浏览很多网站,看看它们是否包含文本框中的文字。 现在我不想添加多线程,因此速度要快得多,最好是我动态添加线程。 这是我的代码,但它没有用,因为它表示范围的索引。我怀疑它是否有效。

Dim clsThreads As New Generic.List(Of System.Threading.Thread)
Dim numberOfthreads As Integer = 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each strLine1 As String In TextBox1.Text.Split({vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)
        For Each strLine2 As String In TextBox2.Text.Split({vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)
            clsThreads(numberOfthreads) = New Thread(Sub() Me.request(strLine1, strLine2))
            clsThreads(numberOfthreads).Name = "Thread: " + numberOfthreads.ToString
            clsThreads(numberOfthreads).IsBackground = True
            clsThreads(numberOfthreads).Start()

            numberOfthreads = numberOfthreads + 1
            If (numberOfthreads.Equals(20)) Then
                numberOfthreads = 0
            End If

        Next
    Next
End Sub

那么,如何以智能方式实现多线程?

这是我的要求:

Public Sub request(ByVal username, ByVal mail)
    Dim req As WebRequest = WebRequest.Create("http://localhost/" + username)
    req.Method = "GET"
    Dim res As WebResponse = req.GetResponse()
    Dim dataStream As Stream = res.GetResponseStream()
    Dim reader As New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    If responseFromServer.Contains(mail) Then
        TextBox3.Text = TextBox3.Text + vbNewLine + username
    End If
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

这样的东西?

Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
    Dim ActiveThreads As New List(Of System.Threading.Thread)
    Delegate Sub delRemoveThread(thread As Threading.Thread)
    Sub removeThread(thread As Threading.Thread)
        If Me.InvokeRequired Then
            Me.Invoke(New delRemoveThread(AddressOf removeThread), thread)
        Else
            ActiveThreads.Remove(thread)
        End If
    End Sub
    Private Sub Button1_Click3(sender As Object, e As EventArgs) Handles Button1.Click
        For i As Integer = 1 To 20
            Dim th As New Threading.Thread(New Threading.ParameterizedThreadStart(Sub()
                                                                                      request("username", "mail")
                                                                                      removeThread(Threading.Thread.CurrentThread)
                                                                                  End Sub))
            th.Start()
        Next
    End Sub
    Public Sub request(ByVal username As String, ByVal mail As String)
        Dim req As Net.WebRequest = Net.WebRequest.Create("http://localhost/" + username)
        req.Method = "GET"
        Dim res As Net.WebResponse = req.GetResponse()
        Dim dataStream As IO.Stream = res.GetResponseStream()
        Dim reader As New IO.StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        If responseFromServer.Contains(mail) Then
            TextBox3.Text = TextBox3.Text + vbNewLine + username
        End If
    End Sub
End Class