Results 1 to 5 of 5

Thread: Question about webbrowser control from another thread

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Location
    Overland Park Kansas
    Posts
    183

    Question about webbrowser control from another thread

    I'm trying to set up a situation where outside of a form I can have form1.webbrowser1 start at www.google.com and navigate to www.yahoo.com where the URL before the navigate is www.google.com and after the navigate is www.yahoo.com.


    Code:
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim testthread As New System.Threading.Thread(AddressOf Form1.dowork)
            site1 = "www.yahoo.com"
            Debug.Print(Form1.WebBrowser1.Url.ToString)
            'this shows www.google.com
            testthread.Start()
            System.Threading.Thread.Sleep(3000)
            'System.Windows.Forms.Application.DoEvents()
            System.Threading.Thread.Sleep(3000)
            Debug.Print(Form1.WebBrowser1.Url.ToString)
            'this shows www.google.com
        End Sub
    This will navigate the webbrowser from another thread using this code on form1:

    Code:
        Public Delegate Sub navigatecallback()
        Public Sub navigate()
            If Me.WebBrowser1.InvokeRequired Then
                Me.WebBrowser1.Invoke(New navigatecallback(AddressOf navigate))
    
            Else
                Me.WebBrowser1.Navigate(site1)
            End If
        End Sub
    
    
        Public Sub dowork()
            navigate()
        End Sub

    Why is is that, when I step through this, my navigating thread does not start when i testthread.start()?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Question about webbrowser control from another thread

    Might help if the declared thread delegate (AddressOf Form1.dowork) and the delegate you actually want to call (navigatecallback) were connected in some way.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Location
    Overland Park Kansas
    Posts
    183

    Re: Question about webbrowser control from another thread

    Quote Originally Posted by dunfiddlin View Post
    Might help if the declared thread delegate (AddressOf Form1.dowork) and the delegate you actually want to call (navigatecallback) were connected in some way.
    How do I connect them? Also how come, when I step through this, the thread starts when I tell it to, but when I run it without stepping through, the thread starts afterwards?
    Last edited by iamcpc; Jan 29th, 2013 at 07:33 PM.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Question about webbrowser control from another thread

    Sorry, I misread you code and didn't spot the dowork Sub tucked in at the bottom. The delay is caused by your sleep on the main thread which stops all thread activity before the start message has been transmitted. If you're going to sleep the main thread there's really no point in putting the browser navigate on another thread anyway. The whole point of threading is to keep the UI active!
    Last edited by dunfiddlin; Jan 29th, 2013 at 08:45 PM.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Location
    Overland Park Kansas
    Posts
    183

    Re: Question about webbrowser control from another thread

    Quote Originally Posted by dunfiddlin View Post
    Sorry, I misread you code and didn't spot the dowork Sub tucked in at the bottom. The delay is caused by your sleep on the main thread which stops all thread activity before the start message has been transmitted. If you're going to sleep the main thread there's really no point in putting the browser navigate on another thread anyway. The whole point of threading is to keep the UI active!
    Is there any way to start a thread, wait a second, and then start another thread? or is there some setting which affects how sleep works?


    I have this multithread loop in one of my applications. In this application there is not pause between the thread starts. After the button click event is done it starts all 5 threads at the same time yet on a new application one starts then after 2 seconds another starts.

    Code:
     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            Dim testthread As New System.Threading.Thread(AddressOf Module1.loop3)
            Dim testthread1 As New System.Threading.Thread(AddressOf Module1.loop3)
            Dim testthread2 As New System.Threading.Thread(AddressOf Module1.loop3)
            Dim testthread3 As New System.Threading.Thread(AddressOf Module1.loop3)
            Dim testthread4 As New System.Threading.Thread(AddressOf Module1.loop3)
            testthread.Start(1)
            System.Threading.Thread.Sleep(2000)
    
            testthread1.Start(2)
            System.Threading.Thread.Sleep(2000)
            testthread2.Start(3)
            System.Threading.Thread.Sleep(2000)
            testthread3.Start(4)
            System.Threading.Thread.Sleep(2000)
            testthread4.Start(5)
            System.Threading.Thread.Sleep(2000)
            Call Module1.loop3(6)
    
        End Sub
    Code:
    Public Sub loop3(ByVal column As Integer)
    
            System.Threading.Thread.Sleep(2000)
    
    
            If xl.visible <> True Then
                xl.visible = True
            End If
    
    
    
            Try
                If Not xl.activeworkbook.name = "Spenddown IN Mdcd.xlsm" Then
                    xl.workbooks.open("C:\Documents and Settings\BMcGuir1\Desktop\Test\spenddown in mdcd.xlsm")
                    GoTo done
                End If
    
            Catch
                xl.workbooks.open("C:\Documents and Settings\BMcGuir1\Desktop\Test\spenddown in mdcd.xlsm")
            End Try
    
    done:
    
    
    
    
    
    
            Dim y As Long
            For y = 1 To 100
                xl.activeworkbook.sheets("sheet1").cells(y, column).value = y
                System.Threading.Thread.Sleep(100)
            Next
            'MsgBox("done")
        End Sub

    How some the sleep(2000) works on one application but not another?
    Last edited by iamcpc; Jan 29th, 2013 at 10:06 PM.

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