簡體   English   中英

如果我從HttpWebRequest和HttpWebResponse發出請求和響應,GUI將掛起

[英]GUI hangs if i make requests and responses from HttpWebRequest and HttpWebResponse

我有一個ac#win Forms程序來檢查URL是否存在。該程序可以正常工作,但是當我使用按鈕啟動該過程時,單擊gui會掛起,直到檢查完成。我基本上是單擊按鈕並執行代碼請幫助我如何實現backgroundworker或其他線程處理,以便我可以訪問我的GUI。

//這是我單擊的按鈕,然后我的GUI掛起(不可訪問),直到兩個功能都執行。

 private void button2_Click(object sender, EventArgs e)   
            {
                performfetch();      
            }

    public void performfetch()
            {
                button2.Enabled = false;
                button1.Enabled = false;
                button2.Visible = false;
                button1.Visible = false;
                progressBar1.Minimum = 0;
                progressBar1.Maximum = int.Parse(label2.Text) - 1;
                for (int i = 0; i < int.Parse(label2.Text); i++)
                {
                    string URL = dataGridView1[0, i].Value.ToString();
                    dataGridView1[1, i].Value = URLExists(URL);
                    progressBar1.Value = i;
                }
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {

                    if (dataGridView1[1, i].Value.ToString() == "Found")
                    {
                        // dataGridView1[1, i].Style.ForeColor = Color.Green;
                        dataGridView1[1, i].Style.ForeColor = Color.Green;
                    }
                    else
                    {
                        dataGridView1[1, i].Style.ForeColor = Color.Red;
                        // dataGridView1[2, i].Style.ForeColor = Color.Red;
                    }
                }
                label6.Text = "Checking Links Completed";
                button2.Enabled = true;
                button1.Enabled = true;
                button2.Visible = true;
                button1.Visible = true;
            }


     static public String URLExists(string url)
            {
                String result = "Not Found";
               try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method = WebRequestMethods.Http.Head;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        response.Close();
                        result = "Found";
                        return result;
                    }

                }
                catch
                {
                                 return result;
               }

                 return result;

            } 

在計時器事件中進行UI更新的一些代碼。

private enum Status { Pending, FoundUnprocessed, FoundProcessed, NotFoundNotProcessed, NotFoundProcessed }
private List<Status>   URLStatuses = new List<bool>() ;
private List<string>   URLs        = new List<string>() ;


private void StartUrlsThreads()
{
  for (int i = 0; i < int.Parse(label2.Text); i++)
  {
     URLs.Add(dataGridView1[0, i].Value.ToString());
     URLStatuses.Add(false) ; 
     Thread newThread = new Thread(new ThreadStart(URLExists));
     newThread.Start(URLs[i]) ;
  }
}

private void URLExists(object urlobj)
{
  try
  {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create((string)urlobj);
    request.Method = WebRequestMethods.Http.Head;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    if (response.StatusCode == HttpStatusCode.OK)
    {
       response.Close();
       URLStatuses[URLs.IndexOf((string)urlobj)]=Status.FoundUnprocessed ;
    }
    else URLStatuses[URLs.IndexOf((string)urlobj)]=Status.NotFoundUnprocessed ;
  }       
  catch { URLStatuses[URLs.IndexOf((string)urlobj)]=Status.NotFoundUnprocessed ; }
}

private void timer1_Tick(object sender, EventArgs e)
{
  for (int i=0;i<URLs.Count;i++) 
  {
    if (URLStatuses[i]==Status.FoundUnprocessed) 
    {
   URLStatuses[i]==Status.FoundProcessed ;
       // update DataGridView && progressbar
    }
    if (URLStatuses[i]==Status.NotFoundUnprocessed) 
    {
   URLStatuses[i]==Status.NotFoundProcessed ;
       // update DataGridView && progressbar
    }
// stop timer if no more pending status
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM