0

私のコードは機能していますが、私の唯一の問題は、ループ中にプログラム全体がフリーズし、ループが完了したときにのみフリーズが停止することです。このフリーズを取り除く方法はありますか? 1から21まで表示したいのですが、フリーズしてすぐに21番しか表示されません。

これが私のコードです。パフォーマンスを微調整するために何かを変更する必要がありますか? 前もって感謝します

  Dim x As Integer = 0
    Do
        Dim POST As String = "authenticity_token=weZnH8V68yQSnQh91UtDZyatys%2FXtPQGN2vooyW4opY%3D&email%5Bto_address%5D=intes2010%40gmail.com&email%5Bfrom_name%5D=Test&email%5Bfrom_address%5D=test%40email.com&email%5Bnote%5D=today+is+a+big+success&email%5Bcopy_yourself%5D=0&id=house-of-pies-466226000"
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse
        Dim tempCookies As New CookieContainer
        request = CType(WebRequest.Create("http://www.yellowpages.com/los-angeles-ca/mip/house-of-pies-466226000/send_email?lid=1000083727260"), HttpWebRequest)
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = POST.Length
        request.Method = "POST"
        request.KeepAlive = True
        request.CookieContainer = tempCookies

        Dim requestStream As Stream = request.GetRequestStream()
        Dim postBytes As Byte() = Encoding.ASCII.GetBytes(POST)
        requestStream.Write(postBytes, 0, postBytes.Length)
        requestStream.Close()

        response = CType(request.GetResponse(), HttpWebResponse)
        tempCookies.Add(response.Cookies)
        Dim postreader As New StreamReader(response.GetResponseStream())

        Dim thepage As String = postreader.ReadToEnd
        RichTextBox1.Text = thepage
        response.Close()

        x = x + 1
        Label1.Text = x
    Loop While (x <= 20)
4

1 に答える 1

1

これをバックグラウンドワーカーで実行する必要があります。

コードが完了するのを待っているため、メインの UI がフリーズするのはそのためです。処理に時間がかかるプロセスがある場合、応答性の高い UI を維持する唯一の方法は、マルチスレッドを使用することです。

http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx - バックグラウンド ワーカーの基本的な使用方法を示しています。

たとえば、progresschanged イベントからのみテキストボックスの値を変更できることに注意してください。

できることは、backgroundworker を開始するボタンを用意してから、上記のコードを Dowork イベントに配置することです。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Try
        If BackgroundWorker1.IsBusy <> True Then
            BackgroundWorker1.RunWorkerAsync()
        End If
    Catch ex As Exception

    End Try
End Sub

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim worker As System.ComponentModel.BackgroundWorker = CType(sender, System.ComponentModel.BackgroundWorker)
    Try
        Dim x As Integer = 0
        Do
            Dim POST As String = "authenticity_token=weZnH8V68yQSnQh91UtDZyatys%2FXtPQGN2vooyW4opY%3D&email%5Bto_address%5D=intes2010%40gmail.com&email%5Bfrom_name%5D=Test&email%5Bfrom_address%5D=test%40email.com&email%5Bnote%5D=today+is+a+big+success&email%5Bcopy_yourself%5D=0&id=house-of-pies-466226000"
            Dim request As HttpWebRequest
            Dim response As HttpWebResponse
            Dim tempCookies As New CookieContainer
            request = CType(WebRequest.Create("http://www.yellowpages.com/los-angeles-ca/mip/house-of-pies-466226000/send_email?lid=1000083727260"), HttpWebRequest)
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = POST.Length
            request.Method = "POST"
            request.KeepAlive = True
            request.CookieContainer = tempCookies

            Dim requestStream As Stream = request.GetRequestStream()
            Dim postBytes As Byte() = Encoding.ASCII.GetBytes(POST)
            requestStream.Write(postBytes, 0, postBytes.Length)
            requestStream.Close()

            response = CType(request.GetResponse(), HttpWebResponse)
            tempCookies.Add(response.Cookies)
            Dim postreader As New StreamReader(response.GetResponseStream())

            Dim thepage As String = postreader.ReadToEnd
            e.Result = CType(thepage, String)
            response.Close()

            x = x + 1
            worker.ReportProgress(x)
        Loop While (x <= 20)
    Catch ex As Exception

    End Try
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Try
        Label1.Text = e.ProgressPercentage.ToString()
    Catch ex As Exception

    End Try
End Sub
Private Sub BackgroundWorker1_Completed(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    Try
        RichTextBox1.Text = e.Result.ToString()
    Catch ex As Exception

    End Try
End Sub

リッチテキスト ボックス内のデータをどのように表示するかによって、これも progresschanged または completed イベントのいずれかで処理する必要があります。

于 2013-07-10T11:38:33.360 に答える