PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
multiple backgroundworkers-VBForums
Results 1 to 4 of 4

Thread: multiple backgroundworkers

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    2

    multiple backgroundworkers

    Hi,

    I am new here.

    I am not a professional programmer so please forgive me if I ask something obvious.

    I am trying to create a program to log data from several devices connected via tcp/ip.

    I want to start each connection in its own thread but I don't know at design time how many devices will be connected.
    I decided to use a list of "backgroundworkers" objects. The problem I have now is how to pass the ipaddress and port to open to each thread and the name of the file to store the data.

    I have searched the internet looking for a similar problem with several backgroundworkers but all examples use only one backgroundworker.

    Regards,
    JM

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: multiple backgroundworkers

    If you need to use multiple threads, then you're just going to need to do it the old-fashioned way and make multiple Threading.Thread objects and point them to the subroutines they're supposed to run (it's actually not hard at all to do).

    BackgroundWorker is pretty much "Threading for Dummies" in that it's an excellent tool to teach a new programmer how to run work on a second thread and is great for simple tasks; but with it's simplicity comes the price of flexibility.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,722

    Re: multiple backgroundworkers

    I would suggest using the ThreadPool. You just call QueueUserWorkItem as many times as you need. The first argument is a delegate for the method you want to execute and the second argument is the data you want to pass to that method. I'm not going to provide a complete solution but in your case you'd start out by defining a type that could contain the data required, i.e. the IP address and the port number:
    vb.net Code:
    1. Friend Structure Address
    2.     Public IPAddress As String
    3.     Public PortNumber As Integer
    4. End Structure
    You'd then write a method that accepted an object of that type and used it. Note that this method must match the signature of the WaitCallback delegate, so it must have one parameter of type object:
    vb.net Code:
    1. Private Sub ProcessAddress(ByVal address As Object)
    2.     If TypeOf address Is Address Then
    3.         Dim realAddress As Address = DirectCast(address, Address)
    4.  
    5.         'Use realAddress here.
    6.     End If
    7. End Sub
    You can now queue that method as many times as you need:
    vb.net Code:
    1. ThreadPool.QueueUserWorkItem(AddressOf ProcessAddress, New Address() With {.IPAddress = "1.2.3.4", .PortNumber = 5})
    2. ThreadPool.QueueUserWorkItem(AddressOf ProcessAddress, New Address() With {.IPAddress = "5.4.3.2", .PortNumber = 1})
    The ThreadPool will queue your work items until a thread pool thread becomes available. That will likely be immediately for each item if the number is low, but if you have a large number of work items then some will wait until others have finished, ensuring you don't grind the system to a halt by trying to run too many tasks at the same time.

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    2

    Re: multiple backgroundworkers

    Thank you both for your suggestions. I will try those.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width