15

Longtime reader, first-time poster here.

My goal: To be able to take advantage of async/await while using the WebBrowser class. As the WebBrowser.Navigate(string url) is an asynchronous method, and you can't examine the html document until the LoadComplete event is fired.

Here is my (working) code so far:

public class AsyncWebBrowser
{
    protected WebBrowser m_WebBrowser;

    private ManualResetEvent m_MRE = new ManualResetEvent(false);

    public void SetBrowser(WebBrowser browser) {
        this.m_WebBrowser = browser;
        browser.LoadCompleted += new LoadCompletedEventHandler(WebBrowser_LoadCompleted);
    }

    public Task NavigateAsync(string url) {
        Navigate(url);

        return Task.Factory.StartNew((Action)(() => {
            m_MRE.WaitOne();
            m_MRE.Reset();
        }));
    }

    public void Navigate(string url) {
        m_WebBrowser.Navigate(new Uri(url));
    }

    void WebBrowser_LoadCompleted(object sender, NavigationEventArgs e) {
        m_MRE.Set();
    }
}

And this previous class now allows me to use the following:

public async void NavigateToGoogle() {
    await browser.NavigateAsync("www.google.com");
    //Do any necessary actions on google.com
}

However, I am wondering if there is a more efficient/proper way of handling this. Specifically, the Task.Factory.CreateNew with the blocking ManualResetEvent. Thanks for your input!

Sean S
  • 485
  • 1
  • 5
  • 14

4 Answers4

12

First off, I think this is a great exercise for learning how async/await works.

You seem to be jumping through hoops in order to make NavigateAsync return a Task. But it doesn't have to return a Task in order to be awaitable! A method that contains an await must return Task, but a method that is awaitable need not return Task; all it has to do is return some type that you can call GetAwaiter on.

You might consider implementing a little type like this:

public struct WebBrowserAwaiter<T>
{
    public bool IsCompleted { get { ... } }
    public void OnCompleted(Action continuation) { ... }
    public T GetResult() { ... }
}

and have NavigateAsync return some type upon which you can call GetAwaiter that returns a WebBrowserAwaiter. No need to build up a Task just to get its GetAwaiter method when you can make your own.

More generally, something you might want to give some thought to is what happens if there is a second call to NavigateAsync while the first one is still navigating?

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • You are very correct, I had spent quite some time to get it to return a Task properly that I could then await. Haha. I will play around with this suggestion and see if I can come up with something. Thanks! – Sean S Dec 22 '11 at 22:58
2

You can use TaskCompletionSource<T> to create a Task and mark it as completed later.

I don't see any alternative for the non-generic task, but as Task<T> derives from Task, you could just use a TaskCompletionSource<object> and set the result to null.

Daniel
  • 15,944
  • 2
  • 54
  • 60
0

I translated Vaibhav's VB code to C#. it's an amazing solution, I don't know why you are disappoint on it.

public class YourClassThatIsUsingWebBrowser : IDisposable
{
    private WebBrowser browser;

    private TaskCompletionSource<BrowserResult> tcs;

    public YourClassThatIsUsingWebBrowser()
    {
        this.browser.DocumentCompleted += AsyncBrowser_DocumentCompleted;

        this.browser.Document.Window.Error += (errorSender, errorEvent) =>
        {
            SetResult(BrowserResult.Exception, errorEvent.Description);
        };
        this.browser.PreviewKeyDown += Browser_PreviewKeyDown;
        this.browser.Navigating += Browser_Navigating;
    }

    private void Browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        tcs = new TaskCompletionSource<BrowserResult>();
    }

    private void Browser_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
        {
            this.browser.Stop();
            SetResult(BrowserResult.Cancelled);
        }
    }

    private void AsyncBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        SetResult();
    }


    public async Task<BrowserResult> NavigateAsync(string urlString)
    {
        this.browser.Navigate(urlString);

        return await tcs.Task;
    }

    private void SetResult(BrowserResult result = BrowserResult.Succeed, string error = null)
    {
        if (tcs == null)
        {
            return;
        }
        switch (result)
        {
            case BrowserResult.Cancelled:
                {
                    tcs.SetCanceled();
                    break;
                }
            case BrowserResult.Exception:
                {
                    tcs.SetException(new Exception(error));
                    break;
                }
            case BrowserResult.Succeed:
            default:
                {
                    tcs.SetResult(result);
                    break;
                }
        }

    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    bool disposed = false;
    protected void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (disposing)
            {
                this.browser.Dispose();
            }
        }
        disposed = true;
    }
}
public enum BrowserResult
{
    Succeed,
    Cancelled,
    Exception,
}
Dongdong
  • 2,208
  • 19
  • 28
-1

I created this class today, with the help of another post on stackoverflow, I want to get the ready webbrowser control without any thread blocking using (Async/Await).

Dim bb = New wbBrowser
Dim wb = Await bb.GetBrowserAsync("http://www.msn.com")

Here is the class:

Imports System.Threading
Imports System.Threading.Tasks

Public Class wbBrowser
    Implements IDisposable

    Dim m_wbBrowser As New WebBrowser
    Dim m_tcs As TaskCompletionSource(Of WebBrowser)

    Public Sub New()
        m_wbBrowser.ScrollBarsEnabled = False
        m_wbBrowser.ScriptErrorsSuppressed = False
        AddHandler m_wbBrowser.DocumentCompleted, Sub(s, args) m_tcs.SetResult(m_wbBrowser)
    End Sub

    Public Async Function GetBrowserAsync(ByVal URL As String) As Task(Of WebBrowser)
        m_wbBrowser.Navigate(URL)
        Return Await WhenDocumentCompleted(m_wbBrowser)
    End Function

    Private Function WhenDocumentCompleted(browser As WebBrowser) As Task(Of WebBrowser)
        m_tcs = New TaskCompletionSource(Of WebBrowser)
        Return m_tcs.Task
    End Function

    Private disposedValue As Boolean
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                m_wbBrowser.Dispose()
            End If
        End If
        Me.disposedValue = True
    End Sub
    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

End Class
Vaibhav J
  • 1,316
  • 8
  • 15