[2005] Adding controls at run time
Hi :)
I have a webbrowser that must navigate a couple of sites and do some actions. Now it does everything sequentially (first www.site1.com, then www.site2.com and so on), but i would like to have one webbrowser per site and do everything in parallel (one browser for site1, one for site2 etc.).
I now that I can make a webbrowser at runtime with the statement dim b1 as new webbrowser, but I could do something like this only if I knew how much sites I will have to navigate.
The structure of the code should be something like this:
1. I read the site list
2. I istantiate n webbrowsers (where n is the number of the sites)
3. The webbrowsers navigate each to it's associated site and do the automation
I use the DocumentCompleted event to do all the automation so the webBrowsers should share it.
Re: [2005] Adding controls at run time
If you want things to happen in parallel then that means you'll have to use multithreading. I'd suggest using BackgroundWorkers for simplicity, but it means that you'll need a BackgroundWorker for each WebBrowser. It would look something like this:
vb Code:
Imports System.ComponentModel
Public Class Form1
Private browsersByWorkers As New Dictionary(Of BackgroundWorker, WebBrowser)
Private Sub CreateWebBrowsers()
Dim urls As String() 'Retrieve URLs and store here.
Dim browser As WebBrowser
For Each url As String In urls
browser = New WebBrowser
'Set properties of browser here and add to UI.
AddHandler browser.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted
browser.Navigate(url)
Next url
End Sub
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
Dim browser As WebBrowser = TryCast(sender, WebBrowser)
If browser IsNot Nothing Then
Dim worker As New BackgroundWorker
Me.browsersByWorkers.Add(worker, browser)
AddHandler worker.DoWork, AddressOf BackgroundWorker_DoWork
AddHandler worker.RunWorkerCompleted, AddressOf BackgroundWorker_RunWorkerCompleted
worker.RunWorkerAsync()
End If
End Sub
Private Sub BackgroundWorker_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs)
Dim worker As BackgroundWorker = TryCast(sender, BackgroundWorker)
If worker IsNot Nothing Then
Dim browser As WebBrowser = Me.browsersByWorkers(worker)
If browser IsNot Nothing Then
'Do your work here. Note that this method is being executed in a worker thread so any access of
'members of the WebBrowser control must be done either via delegation or the ReportProgress method
'and the ProgressChanged event.
End If
End If
End Sub
Private Sub BackgroundWorker_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
Dim worker As BackgroundWorker = TryCast(sender, BackgroundWorker)
If worker IsNot Nothing Then
MessageBox.Show("Operation complete for URL " & Me.browsersByWorkers(worker).Url.AbsolutePath)
End If
End Sub
End Class
Re: [2005] Adding controls at run time
Thank you jmcilhinney :)
I'm going to try this, I'll let you know :)