问题描述:

The situation:

Using VB.NET Framework 3.5 and Visual Studio 2005 i crated a project which uses the WebBrowser object in a working thrat. Breaking it down to the very basics, it looks like:

Dim bWbCompletedDocument As Boolean = False

Sub WbCompletedDocument(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

bWbCompletedDocument = True

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)Handles Me.Load

Dim th As New System.Threading.Thread(AddressOf worker)

th.SetApartmentState(Threading.ApartmentState.STA)

th.Start()

End Sub

Sub worker()

Dim Wb As New WebBrowser

Wb.ScriptErrorsSuppressed = True

AddHandler Wb.DocumentCompleted, AddressOf WbCompletedDocument

Wb.Navigate("google.de")

Dim start As Date = Now

Do While start.AddSeconds(20) > Now

Application.DoEvents()

If (bWbCompletedDocument = True) And (Wb.IsBusy = False) And (Wb.ReadyState = WebBrowserReadyState.Complete) Then

Exit Do

End If

Loop

If bWbCompletedDocument = False Then

Exit Sub

End If

'more code here

End Sub

There is no GUI of the Browser but everything works fine. I navigate to a page catch the DocumentCompleted-event, check the ReadyState and if he IsBuisy and go on with my code.

The Problem

There are sites which start to execute JavaScript-Code when the onLoad-Event occurs. If I just proceed with my .Net code after the Wb-object has loaded the Page, the Wb stops executing JavaScript.

The Question

Is there a way to determine if there is currently JavaScript running in the

WebBrowser-Object and/or to wait for it to finish?

网友答案:

If you are able to add javascript to the page you could raise an event to signal the .NET application at the correct moment. Or you could set a status in javascript and have the .NET application poll it.

See: How to handle javascript events via WebBrowser control for WinForms

相关阅读:
Top