使用线程C#

时间:2019-02-04 08:54:39

标签: c# multithreading

我正在使用多线程来执行某些任务,但是当我启动该过程时,UI挂起,我在另一种解决方案中使用了相同的方法,并且可以正常工作!

这是一个代码段。我也尝试使用HTMLagality,但是我不认为这是原因,该方法使用了正常的http Web请求

使用C#,VS 2015,.Net Framework 4.6.1

 var th = new Thread(() =>
            {
                if (LinksToGetEmailsListView.InvokeRequired)
                {
                    LinksToGetEmailsListView.Invoke((MethodInvoker)delegate ()
                    {
                        foreach (ListViewItem link in LinksToGetEmailsListView.Items)
                        {       
                            #region Extracting Emails from Html Page
                            //instantiate with this pattern 

                            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(link.Text);
                            httpWebRequest.UseDefaultCredentials = true;
                            httpWebRequest.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)";

                            httpWebRequest.Method = WebRequestMethods.Http.Get;
                            httpWebRequest.Accept = "application/json; charset=utf-8";

                            string file;
                            var response = (HttpWebResponse)httpWebRequest.GetResponse();
                            using (var sr = new StreamReader(response.GetResponseStream()))
                            {
                                file = sr.ReadToEnd();
                            }


                            string[] result = GetEmailsFromWebContent(file);
                            foreach (string r in result)
                            {
                                XtraMessageBox.Show(r);
                            }
                            #endregion






                            //   string[] result = GetEmailsFromWebContent(iWeb.Load(link.Text).DocumentNode.OuterHtml);
                            link.Focused = true;
                           // foreach (string email in result)
                            //{
                 //               XtraMessageBox.Show(email);
                            //}

                        }
                    });
                }
                else
                {
                    foreach (ListViewItem link in LinksToGetEmailsListView.Items)
                    {

                        #region Extracting Emails from Html Page
                        //instantiate with this pattern 

                        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(link.Text);
                        httpWebRequest.UseDefaultCredentials = true;
                        httpWebRequest.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)";

                        httpWebRequest.Method = WebRequestMethods.Http.Get;
                        httpWebRequest.Accept = "application/json; charset=utf-8";

                        string file;
                        var response = (HttpWebResponse)httpWebRequest.GetResponse();
                        using (var sr = new StreamReader(response.GetResponseStream()))
                        {
                            file = sr.ReadToEnd();
                        }


                        string[] result = GetEmailsFromWebContent(file);
                        foreach (string r in result)
                        {
                            XtraMessageBox.Show(r);
                        }
                        #endregion
                    }

                }


            });
            th.SetApartmentState(ApartmentState.STA);
            th.Start();

1 个答案:

答案 0 :(得分:3)

您实际上是在UI线程上运行所有代码 ,这就是UI挂起的原因。您启动了辅助线程,但是随后立即进行了InvokeRequired / Invoke检查。好; 是必需的,因为您在辅助线程上。所以...在辅助线程中要做的第一件事是将工作直接推回UI线程。

您可能希望将Invoke推迟到您真正准备好更新UI​​之前,即在最终的XtraMessageBox.Show附近(并且可能在foreach附近)。而且重要的是:从方法的早期删除