Hello Every,
Today I am exploring async operations. In my scenario, I have a windows
form with a webbrowser control. The exercise reads a starting webpage and
populates a Queue with additional urls. From there I process each URL in
queue until the queue is empty. The problems that I am encountering are:
1. The browser appears to fire the DocumentCompleted and the Navigated
event more than once for the same url.
2. I often recieve an Invalid Operation Exception, though I have try
catches everywhere I have code.
Anyone have any ideas where I might be going wrong?
Here is what I have
The website is a custom site that simply loads data from my database.
The database simply represents a relationship of people (Grandparent,
Parent, Child)
Entity: People
PersonID Int
ParentPersonID Int
PersonName Int
The Website has a single page view.aspx and accepts one of two paramters in
the query.
ParentID or PersonID
the base page will contain a listing of links like so
http:\\localhost\asynctest\view.aspx?parentid=1
http:\\localhost\asynctest\view.aspx?parentid=2
http:\\localhost\asynctest\view.aspx?parentid=3
http:\\localhost\asynctest\view.aspx?parentid=4
http:\\localhost\asynctest\view.aspx?parentid=5
Sub sequent pages will then contain links such as
"Parents"
http:\\localhost\asynctest\view.aspx?parentid=7
http:\\localhost\asynctest\view.aspx?parentid=8
"People"
http:\\localhost\asynctest\view.aspx?personid=10
http:\\localhost\asynctest\view.aspx?personid=11
"Persons"
"Doe, John" <-- Does not get added to the queue
On my winform:
event ProcessingComplete(sender as WebBrowser)
dim theQueue as queue
sub ButtonClick(sender, e)
processsite
end sub
sub Processsite
thequeue = new queue
me.wb.navigate(http:\\localhost\asynctest\view.asp x)
end sub
sub wb_documentcomplete(sender, e)
'This fires multiple times on the same page.
try
'Find all links
<psuedo code>
for each ahref as object in sender.document.getelementsbytagname("A")
thequeue.enqueue(ahref.href)
next
catch ex as exception
'Ignore exception, move on for now
console.writeline(ex.tostring)
end try
try
'Find all persons
<psuedo code>
for each personname as object in
sender.document.getelementsbytagname("STRONG")
console.writeline personname.innterText
next
catch ex as exception
'Ignore exception, move on for now
console.writeline(ex.tostring)
end try
'Inform the UI that this processing is complete
raiseevent processingcomplete(sender)
end sub
sub OnProcessingComplete(sender as WebBrowser) handles ProcessingComplete
try
dim strNewLocation as string
strNewLocation = thequeue.dequeue()
sender.navigate(strNewLocation)
catch ex as exception
'Queue is Empty
msgbox "Job's Done"
exit sub
end try
end sub