Results 1 to 14 of 14

Thread: [RESOLVED] Run same class in multiple threads?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Resolved [RESOLVED] Run same class in multiple threads?

    I am trying to create an app that will ping all IP's in a subnet and if they are online attempt to gather WMI information from them. To do this I would like to check multiple IPs at once as one at a time would take ages. So I am trying to do as follows

    VB Code:
    1. sub btnclcick
    2. Dim T1 As Thread
    3.  
    4.         For i = 1 To 255
    5.  
    6.             T1 = New Thread(AddressOf Collect)
    7.             T1.IsBackground = True
    8.             T1.Start()
    9.  
    10.         Next
    11. end sub
    12.  
    13. private sub collect()
    14. 'ping and get WMI
    15. end sub

    Problem is each time the thread is restarted it starts the sub over before it can do anyting, how can I get multiple threads to open that same sub and all work at once?

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Run same class in multiple threads?

    is this 2.0?

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Run same class in multiple threads?

    Use an array of Threads instead of using the same thread over and over
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Re: Run same class in multiple threads?

    VB.NET 2005 .NET framework 2.0

    ok tried this:

    VB Code:
    1. sub btnclcick
    2. Dim T1() As Thread
    3.  
    4.         For i = 1 To 255
    5.  
    6.             T1(i) = New Thread(AddressOf Collect)
    7.             T1(i).IsBackground = True
    8.             T1(i).Start()
    9.  
    10.         Next
    11. end sub
    12.  
    13. private sub collect()
    14. 'ping and get WMI
    15. end sub

    and i get "Object reference not set to an instance of an object." on


    VB Code:
    1. T1(i) = New Thread(AddressOf Collect)

  5. #5
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Run same class in multiple threads?

    2 things

    1. You need to inialize the size of the array
    VB Code:
    1. Dim t1(254) as Thread

    2. Arrays in .Net are 0 based, so your loop will go from 0 - 254
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Unhappy Re: Run same class in multiple threads?

    Ok that fixed that problem, but still same initial problem, each thread seems to cancel what the last was doing out in the sub.

    so when debugging if I step through it might get to the 2nd line of code in the sub and then a new thread will pick up and start over the sub. The first thread never seems to finish the sub...

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Run same class in multiple threads?

    I think .... not sure mind you, but I think the problem is in your delegate.... you are spawing a thread of a localized function.... you may want to move that into its own class... create a new instance of it, then tie that off in it's own thread..... You'll need to make sure you have somekinf of callback so that you know when it's done.

    In each case Collect has the same address... it needs to have it's own address for each thread you spawn off. Moving it into it's own class and creating an instance of that class should solve that problem.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Re: Run same class in multiple threads?

    That sounds about right, but I'm fairly new to the use of a lot of this stuff. what would I need to move into a class (just the collect sub I'm assuming?) and then how do I spawn an instance of that class?

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Run same class in multiple threads?

    Here is a sample of what your code should look like
    VB Code:
    1. Public Class MainClass
    2.     Private Threads(256) As Threading.Thread
    3.     Private PingObjects(256) As PingClass
    4.     Private Sub MySub()
    5.         For I As Integer = 0 To 255
    6.             PingObjects(I) = New PingClass
    7.             Threads(I) = New Threading.Thread(AddressOf PingObjects(I).collect)
    8.             Threads(I).IsBackground = True
    9.             Threads(I).Start()
    10.         Next
    11.     End Sub
    12. End Class
    13. Public Class PingClass
    14.     Public Sub collect()
    15.  
    16.     End Sub
    17. End Class
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Re: Run same class in multiple threads?

    now on:
    VB Code:
    1. Threads(I) = New Threading.Thread(AddressOf PingObjects(I).collect)

    I get
    "Delegate to an instance method cannot have null 'this'."

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Re: Run same class in multiple threads?

    Ok fixed that error, something was screwed within my project file. Moved code to new project and no more error...odd.

    Now the program throws no errors, but the thread does not seem to do anything, I made a test sub to have the thread simple chane the number in a text box and the text box stays blank...any new ideas?

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Re: Run same class in multiple threads?

    So threads can not modify values within another class eh? Any ideas on how to get my threads to output values to my main form?

  13. #13
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Run same class in multiple threads?

    This will update your form as long as the thread belongs to the same class (according to msdn, it's also the thread safe way):
    VB Code:
    1. Private thrd As New Threading.Thread(AddressOf Gonuts)
    2.     Delegate Sub MyDel(ByVal [int] As Integer)
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         thrd.Start()
    6.     End Sub
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.         thrd.Abort()
    10.     End Sub
    11.  
    12.     Private Sub UpdateLabel(ByVal [int] As Int32)
    13.         If Me.Label1.InvokeRequired Then
    14.             Dim d As New MyDel(AddressOf UpdateLabel)
    15.             Me.Invoke(d, New Object() {[int]})
    16.         Else
    17.             Me.Label1.Text = [int].ToString
    18.         End If
    19.     End Sub
    20.  
    21.     Public Sub Gonuts()
    22.         Dim int As Int32 = 0
    23.         While Threading.Thread.CurrentThread.ThreadState <> Threading.ThreadState.Stopped And _
    24.             Threading.Thread.CurrentThread.ThreadState <> Threading.ThreadState.Aborted
    25.             int += 1
    26.             Me.UpdateLabel(int)
    27.         End While
    28.     End Sub
    I'm sure there's a way with reflection for the thread to determine the property name of the parent object so that you can modify the form via seperate object/thread. I'll play a little and see if I can get it to work.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Re: Run same class in multiple threads?

    The problem has changed soo much from the original I am going to begin a new thread... Thanks for all the help.
    Last edited by Thildemar; Aug 2nd, 2006 at 05:29 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