469,279 Members | 2,261 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,279 developers. It's quick & easy.

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

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

Jul 4 '06 #1
1 1132
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

"StuartJ" <st**************@egg.comschreef in bericht
news:11**********************@h44g2000cwa.googlegr oups.com...
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

Jul 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Altramagnus | last post: by
4 posts views Thread by Leslaw Bieniasz | last post: by
204 posts views Thread by Alexei A. Frounze | last post: by
5 posts views Thread by Immortal Nephi | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.