webbrowser1_documentcompleted is firing multiple times

Hi,

The webbrowser1_documentcompleted is firing more than once(3 times).
Eventhough i am checking for ready state to be completed, it is not working.
I am using  this code:

Try
            If (WebBrowser1.ReadyState <> WebBrowserReadyState.Complete) Then
                exit sub
            Else
                WebBrowser1.Document.InvokeScript("GetMap", New String() {"addr1", "addr2"})
            End If

Any suggestions on how to prevent it firing more than once?

Thanks.

rowternetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulHewsCommented:
If there are frames, it will fire for each frame.  One standard trick to trap when it has loaded everything is to check the URL property:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 
    If e.Url = WebBrowser1.Url Then
        MsgBox("Document Completed")
    End If 
End Sub

Open in new window

0
CodeCruiserCommented:
Use the ReadyState

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
       'Completed
    End If
End Sub
0
rowternetAuthor Commented:
Hi,

Using e.url = webbrowser1.url  ----- It does not fire multiple times. But iit is blocking the parameters sent to my map. For some reason the method is not seeing the parameters if i include this line

Using webbrowser1.readystate = webbrowserreadystate.complete  ------ This is not helping. It is firing multiple times. But mymap and other methods are executing properly. This multiple execution is causing a problem as the map is displaying  the same location multiple times.

Any other way to do this?

Thanks
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

PaulHewsCommented:
I am thinking the only other way to do this would be to use some flag to indicate that a location has been added and don't add it again.  It's a hack, but it sounds like there's some odd redirection issue or some other issue that's causing the behaviour, which is not normal.

If for example, the addr1 and addr2 together are supposed to be unique then see code below.

Even better would be if there was a way to check if specific locations had already been added, then don't add them.
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
	Static MyParms As New List(Of String)

	If (WebBrowser1.ReadyState <> WebBrowserReadyState.Complete) Then
		Exit Sub
	Else
		If Not MyParms.Contains("addr1" & "addr2") Then
			MyParms.Add("addr1" & "addr2")
			WebBrowser1.Document.InvokeScript("GetMap", New String() {"addr1", "addr2"})
		End If

	End If
End Sub

Open in new window

0
rowternetAuthor Commented:
Hi PaulHews,

I tried the above one. It did not work.
I have 2 questions:
1) How will i know how many frames are there on the document?
2) Can i call the browser.document.invokescript in the form_load event? Or should i call it only in browser_documentcompleted event?
I tried to call it i the form_load event but it is not working. I need a confimation on this.

Thanks.

0
PaulHewsCommented:
1.  Are you sure frames are being used?  Have you looked at the HTML source of the pages you are loading?

Try:
WebBrowser1.Document.Window.Frames.Count

2.  Form_Load is an event when your windows form loads.  Seems unlikely you could do anything useful here.

>I tried the above one. It did not work.

If something doesn't work, you have to give a better picture of exactly what was tried and how it doesn't work.
0
PaulHewsCommented:
Note that if your script is in a frame, you would need to invoke it from the right document... eg:

WebBrowser1.Document.Window.Frames("frame_with_script").Document.InvokeScript("GetMap", New String() {"addr1", "addr2"})
0
rowternetAuthor Commented:
Hi Paul,

I did not get a a chance to look at them.
Previously all the Javascript functions werelinked. That is i call one function from the wbbrows and that function cals anther function and so on.
I split theJavascript into multiple functions and calling each function in a different button click.
Right now this is working. great We are goign ahead with what we havenow. But, i would revisit this some time later.


I would award the points to you.
How can i do that without accepting solution? Can this be done?

Thanks
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulHewsCommented:
You can click "Accept and Award Points" on your own comment and award points to the experts as desired.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.