1

I am using multithreading to do some tasks but when I start the process, the UI hangs, I am using the same method in another solution and its working fine!

Here is a snippet. I tried to use HTMLagality as well, but I don't think this is the reason, this method uses the normal http web request

Using 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();
5
  • Be more specific - in which code path does it hang? Commented Feb 4, 2019 at 8:57
  • @Nick in the http web request , its get request ! Commented Feb 4, 2019 at 8:59
  • There is what we call UI Thread where all UI-related behaviors should be placed. On your sample code you are creating a new thread and you're trying to perform UI-related actions such as XtraMessageBox.Show(r);. Make sure that it runs on the UI thread instead. Try looking up on how to invoke to the UI thread. Commented Feb 4, 2019 at 9:00
  • I already removed XtraMessagebox from the thread and it didn't solve it ! Commented Feb 4, 2019 at 9:02
  • This is way too much code to post. Try to be concise and only post the Code that actually gives you trouble. It makes the entire thing a lot easier for us to read and understand Commented Feb 4, 2019 at 9:03

1 Answer 1

4

You're actually running all that code on the UI thread, which is why the UI is hanging. You start a secondary thread, but then you immediately do a InvokeRequired / Invoke check. Well; it will be required, because you're on a secondary thread. So... the first thing you do in your secondary thread is push the work right back to the UI thread.

You probably wanted to defer the Invoke until you're actually ready to update the UI, i.e. around the final XtraMessageBox.Show (and possibly the foreach around that). And importantly: remove it from earlier in the method.

Sign up to request clarification or add additional context in comments.

2 Comments

when I removed the http web request the UI didn't hang up ! do you have any idea how I can fix that ! I already removed XtraMessage
@JamesRick like I said: get rid of the premature InvokeRequired / Invoke, so that you aren't doing the http on the UI thread; move the Invoke lower down, when you're actually ready to update the UI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.