ColdPhreze
Programmer
- Apr 15, 2002
- 93
I need to know how I might go about dynamically creating objects with events for use with a variable number of threads?
For example, Lets say I have a class called SquareClass that squares a number, then when done, throws an event so I can handle the result.
I need to be able to have X number of those classes (preferably in an array or arraylist), so that each thread can have it's own object to work with simultaneously. X could be 5 or 5000.
Here's what I want (and this is a simplified thing, just to demonstrate what I need to do) but it obviously doesn't work because you can't declare arrays withevents:
So you see each object would have it's own thread... That way nothing would conflict. Can this be done?
Thanks,
KyferEz
Check out my DeVry Senior Project: and my CarPC Hardware: and my Spam-Fighter forums:
For example, Lets say I have a class called SquareClass that squares a number, then when done, throws an event so I can handle the result.
I need to be able to have X number of those classes (preferably in an array or arraylist), so that each thread can have it's own object to work with simultaneously. X could be 5 or 5000.
Here's what I want (and this is a simplified thing, just to demonstrate what I need to do) but it obviously doesn't work because you can't declare arrays withevents:
Code:
Public Class SquareClass
Public Value As Double
Public Square As Double
Public Event ThreadComplete(ByVal Square As Double)
Public Sub CalcSquare()
Square = Value * Value
RaiseEvent ThreadComplete(Square)
End Sub
End Class
Dim withevents oSquare() as new SquareClass
Dim t() as Thread
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim i = 0
For i = 0 To 100
t(i) = New Thread(AddressOf oSquare(i).CalcSquare)
oSquare(i).Value = i
t(i).Start()
Next i
End Sub
Sub SquareEventHandler(ByVal Square As Double) _
Handles oSquare.ThreadComplete
MsgBox("The square is " & Square)
End Sub
So you see each object would have it's own thread... That way nothing would conflict. Can this be done?
Thanks,
KyferEz
Check out my DeVry Senior Project: and my CarPC Hardware: and my Spam-Fighter forums: