2

Possible Duplicate:
WebBrowser Control in a new thread

I'm developing a Winforms application which has the following controls in it. A textbox, WebBrowser control, and 3 Richtextboxes.

Purpose of this app is that when I enter a URL in the textbox, web browser should navigate me to that page, and then I would be parsing the HTML source of webpage to pickup all the hyperlinks, and again I would be filtering those hyperlinks into valid and invalid hyperlinks.

As of now I'm declaring web browser control in a different thread, and when the navigated event of the web browser fires, then I would be creating one more thread which would do all the parsing stuff which I have mentioned above. The problem is that the application is becoming unresponsive when the web browser is loading the webpage, even though I'm invoking it from a separate thread.

Do we have a workaround for this?

Here is the code through which I'm creating a separate thread for web browser control.

Thread t2 = null;

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
        if (e.KeyChar == (char)13)
        {
            if((textBox1.Text.StartsWith("www") || textBox1.Text.StartsWith("WWW")))
            {
               textBox1.Text = textBox1.Text.Insert(0,"http://");

            }
            if (!(textBox1.Text.EndsWith(".html") || textBox1.Text.EndsWith(".htm")))
            {
                textBox1.Text = textBox1.Text + "/";
            }
            t2 = new Thread(new ParameterizedThreadStart(ActivateBrowser));
            t2.IsBackground = true;
            t2.SetApartmentState(ApartmentState.STA);
            t2.Start(textBox1.Text);
        }
    }

    public void ActivateBrowser(Object URL)
    {
        String url = (string)URL;
        WebBrowserNavigation(url);            
    }

    public delegate void PassURLDelegate(string URL);

    public void WebBrowserNavigation(string URL)
    {
        if (webBrowser1.InvokeRequired)
        {
            this.BeginInvoke(new PassURLDelegate(WebBrowserNavigation), new object[] { URL });
        }
        else
        {
            webBrowser1.Navigate(URL);
        }
    }
Community
  • 1
  • 1
Prithvi
  • 607
  • 10
  • 15
  • Hi Passant, can you please elaborate where I'm duplicating the control. – Prithvi Oct 14 '12 at 16:08
  • I have implemented the code in above mentioned thread, It works awesome, i can use other controls simultaneously while the page is loading in the web browser, everything seems to be working fine, but the problem is when im entering a new url in textbox1, a new webbrowser is getting created in the background and the browser which is already displayed is not getting affected. I want only one browser to be there, and whenever i enter a new url in the textbox1, it should be reflected in the already existing browser. How to solve this problem? – Prithvi Oct 15 '12 at 13:06

0 Answers0