|
-
Aug 1st, 2006, 01:47 PM
#1
Thread Starter
Junior Member
[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:
sub btnclcick
Dim T1 As Thread
For i = 1 To 255
T1 = New Thread(AddressOf Collect)
T1.IsBackground = True
T1.Start()
Next
end sub
private sub collect()
'ping and get WMI
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?
-
Aug 1st, 2006, 01:51 PM
#2
Re: Run same class in multiple threads?
-
Aug 1st, 2006, 03:03 PM
#3
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
-
Aug 1st, 2006, 04:32 PM
#4
Thread Starter
Junior Member
Re: Run same class in multiple threads?
VB.NET 2005 .NET framework 2.0
ok tried this:
VB Code:
sub btnclcick
Dim T1() As Thread
For i = 1 To 255
T1(i) = New Thread(AddressOf Collect)
T1(i).IsBackground = True
T1(i).Start()
Next
end sub
private sub collect()
'ping and get WMI
end sub
and i get "Object reference not set to an instance of an object." on
VB Code:
T1(i) = New Thread(AddressOf Collect)
-
Aug 1st, 2006, 04:42 PM
#5
Frenzied Member
Re: Run same class in multiple threads?
2 things
1. You need to inialize the size of the array
2. Arrays in .Net are 0 based, so your loop will go from 0 - 254
-
Aug 1st, 2006, 04:46 PM
#6
Thread Starter
Junior Member
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...
-
Aug 1st, 2006, 04:57 PM
#7
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
-
Aug 1st, 2006, 05:00 PM
#8
Thread Starter
Junior Member
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?
-
Aug 1st, 2006, 05:11 PM
#9
Re: Run same class in multiple threads?
Here is a sample of what your code should look like
VB Code:
Public Class MainClass
Private Threads(256) As Threading.Thread
Private PingObjects(256) As PingClass
Private Sub MySub()
For I As Integer = 0 To 255
PingObjects(I) = New PingClass
Threads(I) = New Threading.Thread(AddressOf PingObjects(I).collect)
Threads(I).IsBackground = True
Threads(I).Start()
Next
End Sub
End Class
Public Class PingClass
Public Sub collect()
End Sub
End Class
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Aug 1st, 2006, 05:23 PM
#10
Thread Starter
Junior Member
Re: Run same class in multiple threads?
now on:
VB Code:
Threads(I) = New Threading.Thread(AddressOf PingObjects(I).collect)
I get
"Delegate to an instance method cannot have null 'this'."
-
Aug 2nd, 2006, 10:57 AM
#11
Thread Starter
Junior Member
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?
-
Aug 2nd, 2006, 02:11 PM
#12
Thread Starter
Junior Member
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?
-
Aug 2nd, 2006, 02:57 PM
#13
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:
Private thrd As New Threading.Thread(AddressOf Gonuts)
Delegate Sub MyDel(ByVal [int] As Integer)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
thrd.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
thrd.Abort()
End Sub
Private Sub UpdateLabel(ByVal [int] As Int32)
If Me.Label1.InvokeRequired Then
Dim d As New MyDel(AddressOf UpdateLabel)
Me.Invoke(d, New Object() {[int]})
Else
Me.Label1.Text = [int].ToString
End If
End Sub
Public Sub Gonuts()
Dim int As Int32 = 0
While Threading.Thread.CurrentThread.ThreadState <> Threading.ThreadState.Stopped And _
Threading.Thread.CurrentThread.ThreadState <> Threading.ThreadState.Aborted
int += 1
Me.UpdateLabel(int)
End While
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.
-
Aug 2nd, 2006, 04:05 PM
#14
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|