Results 1 to 2 of 2

Thread: I need a way to control what happens!

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    1

    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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: I need a way to control what happens!

    try this:

    vb Code:
    1. private cancelled as boolean = false
    2. private paused as boolean = false
    3.  
    4. Private Sub cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
    5.     cancelled  = true
    6. End Sub
    7. Private Sub pause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pause.Click
    8.     paused = true
    9. End Sub
    10. Private Sub submit_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit_btn.Click
    11.     cancelled = false
    12.     paused = false
    13.     if cancelled then return
    14.     while paused
    15.         application.doevents
    16.     end while
    17.     Call website1(Args)
    18.     application.doevents
    19.     if cancelled then return
    20.     while paused
    21.         application.doevents
    22.     end while
    23.     Call website2(Args)
    24.     'etc
    25. End Sub
    26. Private Sub website1(Args)
    27.     browser.Navigate("http://example.com")
    28.     ' login code here
    29.     ' browser.Navigate("http://example.com/post/here/")
    30.     ' post some data, click some buttons
    31. End Sub
    32. Private Sub website2(Args)
    33.     browser.Navigate("http://example2.com")
    34.     ' login code here
    35.     ' browser.Navigate("http://example2.com/post/here/")
    36.     ' post some data, click some buttons
    37. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width