Results 1 to 3 of 3

Thread: [2005] Adding controls at run time

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    45

    [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Imports System.ComponentModel
    2.  
    3. Public Class Form1
    4.  
    5.     Private browsersByWorkers As New Dictionary(Of BackgroundWorker, WebBrowser)
    6.  
    7.     Private Sub CreateWebBrowsers()
    8.         Dim urls As String() 'Retrieve URLs and store here.
    9.         Dim browser As WebBrowser
    10.  
    11.         For Each url As String In urls
    12.             browser = New WebBrowser
    13.  
    14.             'Set properties of browser here and add to UI.
    15.  
    16.             AddHandler browser.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted
    17.             browser.Navigate(url)
    18.         Next url
    19.     End Sub
    20.  
    21.     Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, _
    22.                                              ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
    23.         Dim browser As WebBrowser = TryCast(sender, WebBrowser)
    24.  
    25.         If browser IsNot Nothing Then
    26.             Dim worker As New BackgroundWorker
    27.  
    28.             Me.browsersByWorkers.Add(worker, browser)
    29.             AddHandler worker.DoWork, AddressOf BackgroundWorker_DoWork
    30.             AddHandler worker.RunWorkerCompleted, AddressOf BackgroundWorker_RunWorkerCompleted
    31.             worker.RunWorkerAsync()
    32.         End If
    33.     End Sub
    34.  
    35.     Private Sub BackgroundWorker_DoWork(ByVal sender As System.Object, _
    36.                                         ByVal e As System.ComponentModel.DoWorkEventArgs)
    37.         Dim worker As BackgroundWorker = TryCast(sender, BackgroundWorker)
    38.  
    39.         If worker IsNot Nothing Then
    40.             Dim browser As WebBrowser = Me.browsersByWorkers(worker)
    41.  
    42.             If browser IsNot Nothing Then
    43.                 'Do your work here.  Note that this method is being executed in a worker thread so any access of
    44.                 'members of the WebBrowser control must be done either via delegation or the ReportProgress method
    45.                 'and the ProgressChanged event.
    46.             End If
    47.         End If
    48.     End Sub
    49.  
    50.     Private Sub BackgroundWorker_RunWorkerCompleted(ByVal sender As Object, _
    51.                                                     ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
    52.         Dim worker As BackgroundWorker = TryCast(sender, BackgroundWorker)
    53.  
    54.         If worker IsNot Nothing Then
    55.             MessageBox.Show("Operation complete for URL " & Me.browsersByWorkers(worker).Url.AbsolutePath)
    56.         End If
    57.     End Sub
    58.  
    59. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    45

    Re: [2005] Adding controls at run time

    Thank you jmcilhinney

    I'm going to try this, I'll let you know

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