473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1213
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum...
21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.