S
StuartJ
Hi,
I'm trying to create a generic threading application where you can pass
in any object and a number of threads, most of it is working but I'm
having trouble creating multiple instances of the object handed down.
The code that I've supplied should simply create msgbox with 10
different messages, but it the ProcessID gets incremented for all the
threads everytime a new object is created.
Can anyone point me in the right direction.....
Thanks in advance
Sub Main()
Dim myThreadRunner As New Threader.Threader
Dim obj As New inherSimpleObj
myThreadRunner.ThreadRunner(10, obj)
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Class Threader
Public Function ThreadRunner(ByVal nT As Integer, ByVal myProcess As
Object)
Dim myobj As Object
myobj = myProcess
Dim t(nT - 1) As Threading.Thread
Dim stiThreads(nT - 1) As clsSimpleObject
Dim X As Integer = 0
Do Until X = (nT)
Dim myNewObj As New object
myNewObj = DirectCast(myProcess, clsSimpleObject)
stiThreads(X) = myNewObj
stiThreads(X).ProcessID = X + 1
t(X) = New Threading.Thread(AddressOf stiThreads(X).Execute)
X += 1
Loop
For Each thrd As Threading.Thread In t
thrd.Start()
System.Threading.Thread.Sleep(100)
Next
End Function
End Class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Class inherSimpleObj
Inherits clsSimpleObject
Overrides Sub Execute()
MsgBox(ProcessID)
End Sub
End Class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Class clsSimpleObject
Private PID As Integer
Overridable Sub Execute()
End Sub
Public Property ProcessID() As Integer
Get
Return PID
End Get
Set(ByVal Value As Integer)
PID = Value
End Set
End Property
End Class
I'm trying to create a generic threading application where you can pass
in any object and a number of threads, most of it is working but I'm
having trouble creating multiple instances of the object handed down.
The code that I've supplied should simply create msgbox with 10
different messages, but it the ProcessID gets incremented for all the
threads everytime a new object is created.
Can anyone point me in the right direction.....
Thanks in advance
Sub Main()
Dim myThreadRunner As New Threader.Threader
Dim obj As New inherSimpleObj
myThreadRunner.ThreadRunner(10, obj)
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Class Threader
Public Function ThreadRunner(ByVal nT As Integer, ByVal myProcess As
Object)
Dim myobj As Object
myobj = myProcess
Dim t(nT - 1) As Threading.Thread
Dim stiThreads(nT - 1) As clsSimpleObject
Dim X As Integer = 0
Do Until X = (nT)
Dim myNewObj As New object
myNewObj = DirectCast(myProcess, clsSimpleObject)
stiThreads(X) = myNewObj
stiThreads(X).ProcessID = X + 1
t(X) = New Threading.Thread(AddressOf stiThreads(X).Execute)
X += 1
Loop
For Each thrd As Threading.Thread In t
thrd.Start()
System.Threading.Thread.Sleep(100)
Next
End Function
End Class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Class inherSimpleObj
Inherits clsSimpleObject
Overrides Sub Execute()
MsgBox(ProcessID)
End Sub
End Class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Class clsSimpleObject
Private PID As Integer
Overridable Sub Execute()
End Sub
Public Property ProcessID() As Integer
Get
Return PID
End Get
Set(ByVal Value As Integer)
PID = Value
End Set
End Property
End Class