Creating an array of objects when the object type is not known at design time

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
 
C

Cor Ligthert [MVP]

Stuart,

Did you visit this newsgroup before and have read some question.

In my idea comes your question almost one or two times a day.

The answers are mostly in it shortest format, it is not easily to get to the
UI in a workerthread.

I know it is not much maybe it helps having this information.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top