View Single Post
  #1 (permalink)  
Old August 30th, 2014, 10:42 AM
atrocious atrocious is offline
Registered User
Points: 10, Level: 1
Points: 10, Level: 1 Points: 10, Level: 1 Points: 10, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default multi threading in http web request

hello every one i have made a proxy checker which sends http web request and checks the proxy weather it responds in a certain time or not i have employed a background worker on it. but i wanna know is it possible to use multiple threads in same process if yes then how to control them using timer

here is the source code of my app

Imports System
Imports System.IO
Imports System.Net
Imports System.Threading.Thread
Imports System.Collections

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False
TrackBar1.Maximum = 10
TrackBar1.Minimum = 1
TrackBar1.SmallChange = 1
Label1.Text = TrackBar1.Value
ComboBox1.Items.Add("0")
ComboBox1.Items.Add("1")
ComboBox1.Items.Add("2")
ComboBox1.Items.Add("3")
ComboBox1.Items.Add("4")
ComboBox1.Items.Add("5")
ComboBox1.Items.Add("6")
Label3.Text = ListBox1.Items.Count()
Label2.Text = ListBox1.Items.Count()
Label5.Text = ListBox2.Items.Count()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
listbox1.items.Clear()
ListBox2.Items.Clear()
Dim fo As New OpenFileDialog
fo.RestoreDirectory = True
fo.Multiselect = True
fo.Filter = "txt files (*.txt)|*.txt"
fo.FilterIndex = 1
fo.ShowDialog()
If Not (fo.FileName = Nothing) Then
Using sr As New StreamReader(fo.FileName)
While sr.Peek <> -1
CheckedListBox1.Items.Add(sr.ReadLine())
Label2.Text = CheckedListBox1.Items.Count()
If TextBox1.Text = "" Then
MsgBox("Enter Url First Then Select Proxies")
End
End If
End While
End Using
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim fs As New SaveFileDialog
fs.RestoreDirectory = True
fs.Filter = "txt files (*.txt)|*.txt"
fs.FilterIndex = 1
fs.ShowDialog()
If Not (fs.FileName = Nothing) Then
Using sw As New StreamWriter(fs.FileName)
For Each line As String In ListBox2.Items
sw.WriteLine(line)
Next
End Using
End If
End Sub

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Label1.Text = TrackBar1.Value
End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub


Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MsgBox(" work completed ")

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
BackgroundWorker1.Dispose()
BackgroundWorker1.CancelAsync()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SyncLock (BackgroundWorker1)
End SyncLock
SyncLock (BackgroundWorker2)
End SyncLock
SyncLock (BackgroundWorker3)
End SyncLock
SyncLock (BackgroundWorker4)
End SyncLock
SyncLock (BackgroundWorker5)
End SyncLock
SyncLock (BackgroundWorker5)
End SyncLock
SyncLock (BackgroundWorker6)
End SyncLock
SyncLock (BackgroundWorker7)
End SyncLock
SyncLock (BackgroundWorker8)
End SyncLock
SyncLock (BackgroundWorker9)
End SyncLock
SyncLock (BackgroundWorker10)
End SyncLock
SyncLock (BackgroundWorker11)
End SyncLock
SyncLock (BackgroundWorker12)
End SyncLock
SyncLock (BackgroundWorker13)
End SyncLock
SyncLock (BackgroundWorker14)
End SyncLock
SyncLock (BackgroundWorker15)
End SyncLock
SyncLock (BackgroundWorker16)
End SyncLock
SyncLock (BackgroundWorker17)
End SyncLock
SyncLock (BackgroundWorker18)
End SyncLock
SyncLock (BackgroundWorker19)
End SyncLock
SyncLock (BackgroundWorker20)
End SyncLock
End Sub

Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker3_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker3.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker4_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker4.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker5_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker5.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker6_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker6.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker7_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker7.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker8_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker8.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker9_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker9.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker10_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker10.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker11_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker11.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker12_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker12.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker13_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker13.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker14_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker14.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker15_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker15.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker16_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker16.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker17_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker17.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker18_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker18.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker19_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker19.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub

Private Sub BackgroundWorker20_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker20.DoWork
Dim myProxy As WebProxy
For Each line As String In CheckedListBox1.Items
Try
myProxy = New WebProxy(line)
Dim r As HttpWebRequest = HttpWebRequest.Create(TextBox1.Text)
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 = TrackBar1.Value * 1000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add("" & line)
Catch ex As Exception
ListBox2.Items.Add("not working" & line)
Label5.Text = ListBox2.Items.Count()
End Try
Sleep(ComboBox1.Text * 1000)
Next
End Sub
End Class