English 中文(简体)
需要帮助添加停止按钮 - > HttpWeb 请求
原标题:Need help adding a stop button --> HttpWebRequest

我有一个程序, 程序有一个按钮叫做“ 研究 ”, 当按下此按钮时, 它从输入文本框中读取, 输入文本框中可以说“ 如何 ” 。

它开始从google调出自动建议, 星号被字母字母中的字母替换 。

我的问题是:

我想要一个按钮来停止搜索 趁它还在动的时候

实现这一点的最佳途径是什么?

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");
        }
最佳回答

您应该使用 < a href=> http://msdn. microsoft.com/ en- us/library/ system. net. httpwebrease.begetresponse%28v=vs. 100%29.aspx" rel = "nofollow" > beginGetResponse , 并通过您的响应处理作为回调参数。 在您的取消按钮中, 您可以调用 < a href=" http://msdn. microsoft. com/ en- us/library/ systems. net. httpwebrequep. abort.aspx" rel=“ nofolpolt " > Abort 方法。

试了一下,但一般应该能行

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签