0

I am using the following code to use a webBrowser control in an aspx website as a class-

public CustomBrowser()
{
    //
    // TODO: Add constructor logic here
    //
}

protected string _url;
string html = "";
public string GetWebpage(string url)
{
    _url = url;
    // WebBrowser is an ActiveX control that must be run in a
    // single-threaded apartment so create a thread to create the
    // control and generate the thumbnail
    Thread thread = new Thread(new ThreadStart(GetWebPageWorker));
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
    string s = html;
    return s;
}

protected void GetWebPageWorker()
{
    using (WebBrowser browser = new WebBrowser())
    {
        //  browser.ClientSize = new Size(_width, _height);
        browser.ScrollBarsEnabled = false;
        browser.ScriptErrorsSuppressed = true;
        browser.Navigate(_url);
        browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        // Wait for control to load page
        while (browser.ReadyState != WebBrowserReadyState.Complete)
            Application.DoEvents();

        html = browser.DocumentText;

    }
}

This works fine however I wish to add other methods to the class that use the same WebBrowser object so adding something like -

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

            if (browser.Document.Body.InnerHtml != null)
            {


                 browse();

            }

    }

private void browse(object sender, EventArgs e)
    {

            browser.Navigate("url");


    }

However I can't access the WebBrowser object as it is not global and also if I set it as global aspx does not allow this is this possible?

Ebikeneser
  • 2,582
  • 13
  • 57
  • 111

1 Answers1

1

You do not need to make it global. You can define a private instance of WebBrowser in your CustomBrowser class like this:

private WebBrowser _browser;
public CustomBrowser()
{
    _browser = new WebBrowser();
}

With this, your new methods can access the same instance of WebBrowser, which is _browser. Your CustomBrowser class needs to implement IDisposable interface because WebBrowser is a disposable object. For example:

public void Dispose()
{
    if (_browser != null)
    {
        _browser.Stop();

        if (!_browser.IsDisposed)
        {
            _browser.Dispose();
        }
    }
}

Reference for implementing IDisposable

Yuan Shing Kong
  • 674
  • 5
  • 15
  • this presents - ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment. Hence why I can't get around using a global webbrowser. Any ideas? – Ebikeneser Aug 11 '14 at 09:44
  • WebBrowser control requires to run on single thread and the problem you get is because you are running it under multithreaded environment. This link might be useful for you: http://stackoverflow.com/questions/1418466/single-threaded-apartment-cannot-instantiate-activex-control – Yuan Shing Kong Aug 11 '14 at 09:53