0

Web サイトでサインアップ オプションを見つけることができるシンプルなツールを作成しました (200 の Web サイト リストは arraylist にあります)。ウェブブラウザを使用していましたが、キャッシュとクッキーに問題があるため、ウェブクライアントに切り替えました。ブレークポイントを配置してデバッグすると正常に動作しますが、通常どおり実行すると、サインアップ オプションのない Web サイトも含まれます。これが私のコードです

private void btnSearch_Click(object sender, EventArgs e)
    {           
            timer1.Enabled = true;
            timer1.Start();
    }

Timer1 コード

 string st;
        private void timer1_Tick(object sender, EventArgs e)
        {
                st = "";
Application.DoEvents();                
                        try
                        {
                            st = lst[dataindex2].ToString();       
                            using (WebClient asyncWebRequest = new WebClient())
                            {
                                asyncWebRequest.DownloadDataCompleted += asyncWebRequest_DownloadDataCompleted;
                                Uri urlToRequest = new Uri(st);
                                asyncWebRequest.DownloadDataAsync(urlToRequest);
                                asyncWebRequest.Dispose();
                            }

                            dataindex2++;
                            if (dataindex2 == lst.Count)
                            {
                                timer1.Stop();                                
                                lblStatus.Text = "Stopped";
                                lblStatus.ForeColor = Color.DarkRed;
                                MessageBox.Show("Search Completed");                                
                            }
                        }
                        catch (Exception ex)
                        {
                            timer1.Stop();                            
                            lblStatus.Text = "Stopped";
                            lblStatus.ForeColor = Color.DarkRed;
                            timer1.Dispose();
                            MessageBox.Show(ex.Message);
                            return;
                        }

asyncWebRequest_DownloadDataCompleted コード:

private void asyncWebRequest_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            timer1.Stop();
            ena();
            lblStatus.Text = "Stopped";
            lblStatus.ForeColor = Color.DarkRed;
            timer1.Dispose();                
            MessageBox.Show(e.Error.Message);                
        }

        if (e.Result != null && e.Result.Length > 0)
        {
            string browsetext = "";
            int = iSuccess = 0;
            browsetext = Encoding.Default.GetString(e.Result);

                    iSuccess = browsetext.IndexOf("Sign up") + 1;
                    if (iSuccess == 0)
                    {

                    }
                    else
                    {

                        listBox1.Items.Add(st);
                        domaincount++;                            
                        lblDomainCount.ForeColor = Color.DarkGreen;
                        lblDomainCount.Text = domaincount.ToString();
                    }
                }
                else
                {
                }
            }
        }
        else
        {
            MessageBox.Show("No data found.");
        }
    }

助けてください。GUI がハングしない代替の Web クライアントがある場合は、提案してください。タイ。

4

1 に答える 1

0

ダウンロードを開始するとすぐに WebClient を破棄します。

asyncWebRequest.DownloadDataAsync(urlToRequest);
asyncWebRequest.Dispose();

GUIをハングアップさせない代替のWebクライアントがある場合は、助けてください

async/await を使用できるように WebClient のラッパーを作成する他の回答を参照してください。HttpClientも代替することができます。

于 2013-08-23T17:42:10.980 に答える