Search code examples
vb.netgeckofx

run multiinstance of geckowebbrowser on multiinstances of 1 form


I want to run a number of instances of 1 form on which i have a dynamically GeckoWebBrowser and some automated tasks but every time i create a new instance of form other GeckoWebBrowser become inactive and only the latest created GeckoWebBrowser works Help!

    Dim f As New Form2
    f = New Form2
    f.Show()

my Code on Form2

    Imports Gecko
Public Class Form3
    Friend WithEvents w As New GeckoWebBrowser
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        w = New GeckoWebBrowser
        Dim n As Integer = 100
        For i = 0 To n
            w.Navigate("http://google.com")
            'do some stuff here
        Next
    End Sub
End Class

Solution

  • I was able to get the same behavior as your. I found a solution. You will probably need to adapt it to your need. I do not know why it behaves like this, but it has something to do with the application messages pump. Anyway, here is how I got it to work.

    Create Form1 with a single button to launch Form2 instances.

    enter image description here

    And the code:

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2
            f.Show()
        End Sub
    End Class
    

    Create Form2 with the Gecko WebBrowser, and a button that will start the naviagation in the WebBrowser.

    enter image description here

    And the code:

    Imports System.Threading
    
    Public Class Form2
    
        Private _stack As New Stack(Of String)
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For i = 0 To 100
                _stack.Push("http://google.com")
                _stack.Push("https://www.wikipedia.org/")
            Next
            GeckoWebBrowser1.Navigate(_stack.Pop())
        End Sub
    
        Private Sub GeckoWebBrowser1_DocumentCompleted(sender As Object, e As Gecko.Events.GeckoDocumentCompletedEventArgs) Handles GeckoWebBrowser1.DocumentCompleted
            Thread.Sleep(500)
            GeckoWebBrowser1.Navigate(_stack.Pop())
        End Sub
    
    End Class
    

    Open several instances of Form2 by clicking the New Form button on Form1. Click the Start button on Form2. This will populate a stack of urls to be navigated to, and navigate to the first url on the stack. Once navigation to each link is completed, the DocumentCompleted event handler triggers the navigation to the next link.

    As I said, you will have to adapt this solution to your needs.