0

I'm having a problem with the WebBrowser control. I have added it to one of the windows, but it doesen't load the page that i navigate to. I want to access to the control from the other windows, so i made public methods like Navigate etc. I have tried adding WebBrowser to other forms and it seems to work normally. It worked on this window when it was without any added code. I'm using the AutoResetEvent , so when the site would load it would continue the program. Could anyone tell me where could be the problem in this code?

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }
    readonly AutoResetEvent thread1Step = new AutoResetEvent(false);
    public void EnterForm(string ElementId, string value)
    {
        HTMLDocument document = (HTMLDocument)TempBrowser.Document;
        document.getElementById(ElementId).innerText = value;
    }
    public void Navigate(string url)
    {
        TempBrowser.Navigate(url);
        thread1Step.WaitOne();
        thread1Step.Reset();
    }
    public void PressButton(string id)
    {
        HTMLDocument doc = (HTMLDocument)TempBrowser.Document;
        IHTMLElement btn = doc.getElementById(id);
        if (btn != null)
        {
            btn.click();
        }
    }
    public void Scroll(int n)
    {
        HTMLDocument doc = (HTMLDocument)TempBrowser.Document;
        doc.parentWindow.scroll(0, n);
    }
    private void TempBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        thread1Step.Set();
    }
    public void CallFunction(string Funct)
    {
        TempBrowser.InvokeScript(Funct);

    }
}
Gregor Rant
  • 346
  • 2
  • 11
  • You are blocking the thread which performs the navigation with `thread1Step.WaitOne();`. So LoadCompleted can never be invoked... See for example http://stackoverflow.com/a/20958546/932418 (It is a WinForm sample but can easily be converted to WPF) – L.B Oct 04 '16 at 19:13
  • @L.B I'm using threading for the first time, but i've somehow managed to get your code working in WPF. But my main problem still persists. `WebBrowser` doesen't load, so everytime i try to get the document from it, the document is null. – Gregor Rant Oct 04 '16 at 19:58

1 Answers1

1

I prepared an async code for WPF based on my other answer ...

public static class MyExtensions
{
    public static Task NavigateAsync(this WebBrowser browser, Uri  uri)
    {
        var tcs = new TaskCompletionSource<object>();
        LoadCompletedEventHandler loadCompleted = null;

        loadCompleted = (s, e) =>
        {
            browser.LoadCompleted -= loadCompleted;
            tcs.SetResult(e.WebResponse);
        };

        browser.LoadCompleted += loadCompleted;
        browser.Navigate(uri);

        return tcs.Task;
    }
}

Now you can remove thread1Step and TempBrowser_LoadCompleted method. Just use

await TempBrowser.NavigateAsync(url);
DoYourWork(); //At this point your page is loaded. Read its content...
Community
  • 1
  • 1
L.B
  • 114,136
  • 19
  • 178
  • 224