2017-11-01 6 views
0

私はあなたにエラーがないコードを表示します!しかし、マルチスレッドシステムはうまくいっていません。スレッドは、1つずつ、次のスレッドを初期化する前に、スレッドが完了するのを待っているようです。どうすればこの問題を解決できますか?問題のマルチスレッドプロキシチェッカーはうまく動作しません

Imports System.Threading 
Imports System.IO 
Imports System.Net 

Public Class Form1 
Dim proxies As New List(Of String) 
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    CheckForIllegalCrossThreadCalls = False 
End Sub 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim threads As Integer = NumericUpDown1.Value 
    ThreadPool.SetMaxThreads(threads, threads) 
    ThreadPool.SetMinThreads((threads/2), (threads/2)) 
    For Each proxy In proxies 
     ThreadPool.QueueUserWorkItem(AddressOf check) 
    Next 
End Sub 
Public Sub check() 
    Dim myProxy As WebProxy 
    For Each proxy As String In proxies 
     Try 
      myProxy = New WebProxy(proxy) 
      Dim r As HttpWebRequest = WebRequest.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 = 3000 
      r.Proxy = myProxy 
      Dim re As HttpWebResponse = r.GetResponse() 
      ListBox1.Items.Add("Working Proxy: " & proxy) 
     Catch ex As Exception 
      ListBox1.Items.Add("Unresponsive Proxy: " & proxy) 
     End Try 
    Next 
End Sub 

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    Dim fo As New OpenFileDialog 
    fo.RestoreDirectory = True 
    fo.Multiselect = False 
    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 
       proxies.Add(sr.ReadLine()) 
      End While 
     End Using 
    End If 
End Sub 
End class 

P.S:公共コード

+0

While While sr.Peek <> -1'を 'While Not sr.EndOfStream'に置き換えてください。 https://msdn.microsoft.com/en-us/library/system.io.streamreader.endofstream(v=vs.110).aspx –

+0

まだ動作していません... :( – Giuseppe

+0

ああ、私は言っていないそれはあなたの問題を解決するだろう。私は 'EndOfStream'のチェックがはるかに明確な方法であることを指摘していた...ストリームの終わりを確認する –

答えて

0

少なくとも一部は、あなたのButton1ハンドラが全体のプロキシリストを反復処理し、各プロキシのスレッドを作成し、check方法されていることです。

For Each proxy In proxies 
    ThreadPool.QueueUserWorkItem(AddressOf check) 
Next 

そして、あなたのcheck方法で:

For Each proxy As String In proxies 
    Try 
     ` other code here 
    Catch 
     ... 
    End Try 
Next 

だから、すべてのプロキシがすべてのスレッドによって確認されることをあなたはあなたのButton1ハンドラを持っている、です。

checkメソッドにパラメータを取らせる必要があります。それはチェックする文字列です。だから、次のようになります。

Public Sub check(stateInfo as Object) 
    Dim proxyString as String = CType(stateInfo, String) 
    Dim myProxy As WebProxy 
    Try 
     myProxy = New WebProxy(proxyString) 
     Dim r As HttpWebRequest = WebRequest.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 = 3000 
     r.Proxy = myProxy 
     Dim re As HttpWebResponse = r.GetResponse() 
     ListBox1.Items.Add("Working Proxy: " & proxyString) 
    Catch ex As Exception 
     ListBox1.Items.Add("Unresponsive Proxy: " & proxyString) 
    End Try 
End Sub 

そして、あなたのButton1ハンドラで:また

For Each proxy In proxies 
    ThreadPool.QueueUserWorkItem(AddressOf check, proxy) 
Next 

を、私は強くあなたが違法クロススレッド呼び出しのチェックを無効にしないことを示唆しています。それは、最近あなたの一人を傷つけるでしょう。これらの呼び出しに同期を使用します。

+0

あなたの素晴らしい返信をありがとう!しかし、 "プロキシは宣言されていません" – Giuseppe

+0

@Giuseppe:私のエラー。私はそれを 'proxyString'に変更しました。私の更新されたコードを見てください。 –

+0

ありがとう、まだ今働いています! – Giuseppe

関連する問題