2012-05-25 43 views
0

我有当按下该按钮,它从一个输入文本框,可以说读取有一个名为“研究”按钮,程序“如何*”需要帮助增加一个停止按钮 - > HttpWebRequest的

它开始从谷歌说拉汽车的建议,星号被字母表中的每个字母取代。

我的问题是:

我想要一个按钮来停止搜索。它在运动中。

完成此操作的最佳方法是什么?

private void btnResearch_Click(object sender, RoutedEventArgs e) 
     {    
      if (string.IsNullOrWhiteSpace(txtResearch.Text.Trim())) 
      { 
       MessageBox.Show("Please enter a search query", "Oops", MessageBoxButton.OK, MessageBoxImage.Exclamation); 
       return;    
      } 
      //Robin: Temporal fix to the multi asterisk 
      if (txtResearch.Text.Trim().Count(s => s == '*') > 1) 
      { 
       MessageBox.Show("Too many asterisk", "Oops", MessageBoxButton.OK, MessageBoxImage.Exclamation); 
       return;    
      } 

      if (!ValidateLimitations()) 
      { 
       return; 
      } 

      ((LongTailBot.MainWindow)App.Current.MainWindow).PlaySound("button_double_click.wav"); 

      this.chbSelAll.IsChecked = false; 
      List<KeywordItem> keyList = new List<KeywordItem>(); 
      this.dgrResearch.ItemsSource = null; 

      List<string> seetKeywords = SetupSeetKeywords(txtResearch.Text.Trim().Replace(" ", "%20")); 
      if (seetKeywords.Count() < 1) 
       return; 

      foreach (string seetKey in seetKeywords) 
      { 
       this.txbResearch.Text = (seetKeywords.IndexOf(seetKey) + 1).ToString() 
        + "/" + seetKeywords.Count() + " " + seetKey.Replace("%20", " "); 
       Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, 
                 new Action(delegate { })); 

       HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.GetRequestUriString(seetKey)); 
       request.Method = "GET"; 
       request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)"; 
       WebResponse response = request.GetResponse(); 

       if (((HttpWebResponse)response).StatusDescription.Trim().ToUpper() == "OK") 
       { 
        using (Stream dataStream = response.GetResponseStream()) 
        { 
         using (StreamReader reader = new StreamReader(dataStream)) 
         { 
          string responseFromServer = reader.ReadToEnd(); 

          var keywordsFound = this.GetKeywordsFound(responseFromServer); 
          foreach (var key in keywordsFound) 
          { 
           keyList.RemoveAll(k => k.Keyword == key);                 
           keyList.Add(new KeywordItem() { Checked = false, Keyword = key }); 

           Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, 
              new Action(delegate { }));         
          } 

         } 
        } 
        if (keyList.Count > 0) 
        { 
         dgrResearch.ItemsSource = null; 
         dgrResearch.ItemsSource = keyList; 
         dgrResearch.ScrollIntoView(dgrResearch.Items[dgrResearch.Items.Count - 1]);       
        } 
       } 

       response.Close();     
      } 

      this.txbResearch.Text = this.dgrResearch.Items.Count.ToString() + " POPULAR PHRASES AVAILABLE!";    
      this.chbSelAll.IsChecked = dgrResearch.Items.Count > 0? true : false; 
      ((LongTailBot.MainWindow)App.Current.MainWindow).PlaySound("abstractbtn.wav"); 
     } 

回答

1

您应该使用BeginGetResponse方法并将响应处理作为回调参数传递。然后在您的取消按钮中,您可以拨打Abort方法。

没有尝试,但通常应该工作。