I need a way to control what happens!
This is how my code is structured:
Code:
Private Sub submit_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit_btn.Click
Call website1(Args)
Call website2(Args)
End Sub
Private Sub website1(Args)
browser.Navigate("http://example.com")
' login code here
' browser.Navigate("http://example.com/post/here/")
' post some data, click some buttons
End Sub
Private Sub website2(Args)
browser.Navigate("http://example2.com")
' login code here
' browser.Navigate("http://example2.com/post/here/")
' post some data, click some buttons
End Sub
So I need a pause and a stop button. Even if I call stop on the browser, the code still runs. That troubles me. I have like 40 sub (website1, website2.... website30), so this takes a while!
I search around and found about threading (YES, I AM A BEGINNER AT THIS) and tried to use BackgroundWorker like so:
Code:
Private Sub submit_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit_btn.Click
BGW.RunWorkerAsync()
End Sub
Private Sub BGW_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGW.DoWork
Call website1(Args)
Call website2(Args)
End Sub
This time I get an error:
Code:
---------------------------
MyProgram
---------------------------
Unable to get the window handle for the 'WebBrowser' control. Windowless ActiveX controls are not supported.
---------------------------
After searching again I found that there is a problem with the Webbrowser control and some STA state for the thread. The solution was something like Invoke. I tried that too:
Code:
Private Delegate Sub starter()
Private Sub testingBGW()
Call website1(Args)
Call website2(Args)
End Sub
Private Sub BGW_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGW.DoWork
BeginInvoke(New starter(AddressOf testingBGW))
End Sub
Private Sub BGW_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGW.RunWorkerCompleted
results.Text = "Done!"
End Sub
This time it worked, I don't know if in a different thread, but still doesn't do what i need! It seems it fires the testingBGW() sub, but exactly after that it finishes, so the reusults.Text gets Done!. So that's no good, because in fact, even the first website in the website1 call didn't finish loading. I guess the background worker invokes then quits(ends). At least I need to stay up until the Invoke is finished or something.
P.S. I really don't understand some of these classes and functions, so bare with me, please explain :(
Re: I need a way to control what happens!
try this:
vb Code:
private cancelled as boolean = false
private paused as boolean = false
Private Sub cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
cancelled = true
End Sub
Private Sub pause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pause.Click
paused = true
End Sub
Private Sub submit_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit_btn.Click
cancelled = false
paused = false
if cancelled then return
while paused
application.doevents
end while
Call website1(Args)
application.doevents
if cancelled then return
while paused
application.doevents
end while
Call website2(Args)
'etc
End Sub
Private Sub website1(Args)
browser.Navigate("http://example.com")
' login code here
' browser.Navigate("http://example.com/post/here/")
' post some data, click some buttons
End Sub
Private Sub website2(Args)
browser.Navigate("http://example2.com")
' login code here
' browser.Navigate("http://example2.com/post/here/")
' post some data, click some buttons
End Sub