多线程挂起UI?

时间:2019-02-14 11:05:07

标签: c# multithreading user-interface

我正在使用多线程来使任务更快,更流畅,当结果增加到richtextbox中时,UI开始挂起,不知道为什么,我在线程中创建了一个Web浏览器,并在单线程类型中做了其他事情!

将线程用作STA(单线程类型)

这是代码段!

  foreach (string line in URLLMemoRichTxt.Lines)
                {
                    string href = line;
                    if (href.Trim() != string.Empty)
                    {
                        //XtraMessageBox.Show(href);
                        if (StopGettingInnerLink == true)
                        {
                            AddLog("Getting links has been stopped successfully!");
                            StopGettingInnerLink = true;
                            break;
                        }
                        else if (StopGettingInnerLink == false)
                        {
                            AddLog("Getting links from " + href);
                            runBrowserThread( new Uri(href));
                            await Task.Delay(5000);
                            AddLog("Giving the tool some rest for 5 seconds ! ");
                        }
                    }
                }


     private void runBrowserThread(Uri url)
        {
             browserth = new Thread(() => {
                var br = new WebBrowser();
                br.ScriptErrorsSuppressed = true;
                br.DocumentCompleted += browser_DocumentCompleted;
                br.Navigate(url);
                Application.Run();
            });
            browserth.SetApartmentState(ApartmentState.STA);
            browserth.Start();

        }

      void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var br = sender as WebBrowser;
            string currentURL = br.Url.ToString();
            if (br.Url == e.Url)
            {


                HtmlElementCollection acollection = br.Document.GetElementsByTagName("a");
                foreach (HtmlElement a in acollection)
                {

                    string href = a.GetAttribute("href");

                    if (URLLMemoRichTxt.InvokeRequired)
                    {
                        URLLMemoRichTxt.Invoke((MethodInvoker)delegate ()
                        {
                            if (!URLList.Contains(href) && href.Trim() != string.Empty && !href.Contains(".jpg") && !href.Contains(".png") && !href.Contains(".gif") && !href.Contains(".jpeg"))
                            {
                                URLList.Add(href);
                                // URLListView.Items.Add(href);
                                // adding new link ino listview ! 
                                //   URLListCountLBL.Text = URLListView.Items.Count.ToString();
                                URLLMemoRichTxt.Text += href + "\n";

                                URLListCountLBL.Text = URLLMemoRichTxt.Lines.Length.ToString();
                                //   runbrowserinthread(href);

                            }
                        });

                    }
                    else
                    {
                        if (!URLList.Contains(href) && href.Trim() != string.Empty && !href.Contains(".jpg") && !href.Contains(".png") && !href.Contains(".gif") && !href.Contains(".jpeg"))
                        {
                            URLList.Add(href);
                            //                                                      URLListView.Items.Add(href);
                            URLLMemoRichTxt.Text += href + "\n";

                            URLListCountLBL.Text = URLLMemoRichTxt.Lines.Length.ToString();
                            //     GetInnerLink(href);
                        }
                    }

                }

                AddLog("All  links has been scrapped successfully for \r\n" + currentURL);

                Application.ExitThread();   // Stops the thread
            }
        }

1 个答案:

答案 0 :(得分:0)

我自己已经找到了解决方案:

已替换:

                    URLLMemoRichTxt.Text += href + "\n";

使用:

  URLLMemoRichTxt.AppendText(Environment.NewLine + href);