首页 > 解决方案 > 如何制作一个恒定的代理检查器

问题描述

我已经完成了修改公共代码的多线程代理检查器。但我需要它来不断检查文件。这个想法是该文件将经常更新,并且代理检查器将始终在执行中,检查更新文件中的代理。然后它将代理(所有代理,而不仅仅是工作代理)导出到 .properties 文件中。问题是:我无法读取文件,检查代理并导出它,因为我不断收到错误“文件已被使用”。另外,即使它是多线程的,我也觉得它很慢......我真的需要帮助。

我正在使用的代码:

Dim proxies As New List(Of String)

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(proxyString & "={""proxy"":true}")
        Catch ex As Exception
            ListBox1.Items.Add(proxyString & "={""proxy"":false}")
        End Try
    End Sub



Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        Using sr As New StreamReader("D:\Arquivos PC\Desktop\proxytests.txt")
            While Not sr.EndOfStream
                proxies.Add(sr.ReadLine())
            End While
        End Using

        Timer2.Start()
    End Sub

'The problem with the timers is, it doesn't wait the task to be finished before starting the next task (timer)...


Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Timer2.Stop()
        Dim threads As Integer = 100
        ThreadPool.SetMaxThreads(threads, threads)
        ThreadPool.SetMinThreads((threads / 2), (threads / 2))
        For Each proxy In proxies
            ThreadPool.QueueUserWorkItem(AddressOf check, proxy)
        Next

        Timer3.Start()
    End Sub

Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
        Timer3.Stop()
        If (ListBox1.Items.Count > 0) Then
                Using sw As New StreamWriter("D:\Arquivos PC\Desktop\file.properties")
                    For Each line As String In ListBox1.Items
                        sw.WriteLine(line)
                    Next
                End Using
            End If

        Timer4.Start()
    End Sub

Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
        Timer4.Stop()
        ListBox1.Items.Clear()
        proxies.Clear()
        Timer1.Start()
    End Sub

标签: vb.net

解决方案


推荐阅读