Waiting for a web browser document, with no way to log out

I want to wait for the web browser to fully load the page and do something; However, I do not want to leave the main method, because I will return a bool depending on the value read for the browser page.

Here is what I tried, but obviously I leave the main method

    public async Task<bool> Commit()
    {
        try
        {
            Browser = new WebBrowser { ScriptErrorsSuppressed = true };

            Browser.Navigate(Server);
            Browser.DocumentCompleted += DocumentCompleteMethod;

//HERE I WANNA RETURN TRUE,FALSE DEPENDING ON THE VALUE I WILL READ FROM THE WEB PAGE
        }
        catch (Exception ex)
        {
            return false;
        }
        return true;
    }

private void DocumentCompleteMethod(object sender, WebBrowserDocumentCompletedEventArgs e)
{

}

      

+3


source to share


3 answers


You can change your code to this.



  public async Task<bool> Commit()
        {
            try
            {
                var Browser = new WebBrowser { ScriptErrorsSuppressed = true };

                Browser.Navigate(Server);
                Browser.DocumentCompleted += DocumentCompleteMethod;

               while (Browser.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }

   //At this point you can access the document as it is sure to be loaded.
   string title = Browser.Document.Title;

    //HERE I WANNA RETURN TRUE,FALSE DEPENDING ON THE VALUE I WILL READ FROM THE WEB PAGE
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

    private void DocumentCompleteMethod(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

    }

      

+1


source


This is a common problem with asynchronous programming. Your usual synchronous processes are not working as you would like.

Consider restructuring your process using asynchronous methodologies. Don't return a value, don't start a process, and don't let the process handle the action. For example, let's DocumentCompleteMethod

take an action based on the results of the page.



If you really have to make an asynchronous process conform to synchronous methods, then consider using thread synchronizers such as AutoResetEvent

. Jon Skeet has written an excellent answer to a question here on StackOverflow regarding executing the WebBrowser.Navigate method on a stream.

+2


source


I created this class today, using another post on stackoverflow, I want to get a ready-made webbrowser control without blocking threads 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

      

+1


source







All Articles