I'm trying to make a multi-threaded proxy checker.
I already have a kinda working checker but it gives me this error: Object reference not set to an instance of an object.
I don't know how to solve it and I also don't know why its giving me that error.
Can someone explain me why it is giving me this error and how I should handle it?
Here is my code:
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
error occures at this line: ListBox1.Items.RemoveAt(location)
So when you access a listbox with alot of threads its kinda unstable when deleting an index because every other item gets an new index wich is the old index - 1
I think this is kinda unstable so as Yacoub Massad mentioned in his comment its better to keep the data in there. And that's exactly what I did.
I found out that I dont need to delete the data. I simply need to go to another index wich isn't edited or used by any other thread. What did I do to solve the problem:
TrackBar1.value is the thread count.
This way the threads are never at the same index or item. This answer is based on what I think that caused the problem.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments