多线程代理检查器对象未设置为对象的实例

时间:2015-12-29 20:12:39

标签: vb.net multithreading

我正在尝试制作多线程代理检查程序。

我已经有一个有点工作的检查器,但它给了我这个错误: 对象引用未设置为对象的实例。

我不知道如何解决它,我也不知道为什么它会给我这个错误。

有人可以解释一下为什么它会给我这个错误以及我该如何处理它?<​​/ p>

这是我的代码:

    Private Sub checkProxys(ByVal location As Integer)
    CheckForIllegalCrossThreadCalls = False
    Dim proxy As String

    Do Until location >= ListBox1.Items.Count - 1

        Dim countitems = ListBox1.Items.Count - 1
        ' Console.WriteLine("Location: " & location & "|  Listbox Items: " & ListBox1.Items.Count.ToString & "|  Items - 1: " & countitems.ToString)

        Dim myProxy As WebProxy
        proxy = ListBox1.Items(location).ToString
        ListBox1.Items.RemoveAt(location)

        Try
            myProxy = New WebProxy(proxy)
            Dim r As HttpWebRequest = HttpWebRequest.Create("http://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 = TrackBar2.Value
            r.Proxy = myProxy
            Dim re As HttpWebResponse = r.GetResponse()
            ListBox2.Items.Add(proxy)
            Label9.Text = "Working proxy's: " & ListBox2.Items.Count.ToString
            Label4.Text = "Proxy's loaded: " & ListBox1.Items.Count
        Catch
            ListBox3.Items.Add(proxy)
            Label10.Text = "Unresponsive proxy's: " & ListBox3.Items.Count.ToString
            Label4.Text = "Proxy's loaded: " & ListBox1.Items.Count
        End Try
    Loop

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    If ListBox1.Items.Count < 1 Then
        MsgBox("Make sure to import a proxy list first.")
    Else
        For x As Integer = 1 To TrackBar1.Value
            Dim checkProxyThread As New Threading.Thread(AddressOf checkProxys)
            checkProxyThread.Start(x)
        Next
    End If

End Sub
此行发生

错误:ListBox1.Items.RemoveAt(location)

1 个答案:

答案 0 :(得分:1)

所以当你访问一个包含大量线程的列表框时,它在删除索引时会有点不稳定,因为每个其他项都会得到一个新索引,这是旧索引 - 1

我认为这有点不稳定,因为Yacoub Massad在评论中提到更好地保留数据。而这正是我所做的。

我发现我不需要删除数据。我只需要转到另一个未被任何其他线程编辑或使用的索引。我该怎么做才能解决这个问题:

  1. 删除行:ListBox1.Items.RemoveAt(location)
  2. 在循环结束之前,我写了这一行:location = location + TrackBar1.Value
  3. TrackBar1.value是线程数。

    这样线程永远不会在同一个索引或项目上。 这个答案是基于我认为导致问题的原因。

相关问题