C#网络客户端工作不正常

C#网络客户端工作不正常,c#,browser,webclient,webrequest,C#,Browser,Webclient,Webrequest,我创建了一个简单的工具,可以在网站中找到注册选项(arraylist中有200个网站列表)。 我使用的是webbrowser,但它存在缓存和cookie问题,所以我切换到webclient。当我设置断点并调试时,它可以正常工作,但当我正常运行时,它还包括那些没有注册选项的网站。 这是我的密码 private void btnSearch_Click(object sender, EventArgs e) { timer1.Enabled =

我创建了一个简单的工具,可以在网站中找到注册选项(arraylist中有200个网站列表)。 我使用的是webbrowser,但它存在缓存和cookie问题,所以我切换到webclient。当我设置断点并调试时,它可以正常工作,但当我正常运行时,它还包括那些没有注册选项的网站。 这是我的密码

private void btnSearch_Click(object sender, EventArgs e)
    {           
            timer1.Enabled = true;
            timer1.Start();
    }
定时器1代码

 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\u下载数据完成代码:

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.");
        }
    }

请提供帮助,如果有任何webclient的替代品不挂gui,请提出建议。ty.

您可以在开始下载后立即处理WebClient

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

请提供帮助,如果有任何webclient的替代品不挂起gui


请参阅为WebClient创建的包装器,以便能够使用async/await。也可以作为替代品。

所以我不应该处理它?@user1445345只有在下载完成后才能处理。