473,698 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Thread er
Dim obj As New inherSimpleObj
myThreadRunner. ThreadRunner(10 , obj)
End Sub

~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~
Public Class Threader
Public Function ThreadRunner(By Val nT As Integer, ByVal myProcess As
Object)

Dim myobj As Object
myobj = myProcess

Dim t(nT - 1) As Threading.Threa d
Dim stiThreads(nT - 1) As clsSimpleObject
Dim X As Integer = 0
Do Until X = (nT)
Dim myNewObj As New object
myNewObj = DirectCast(myPr ocess, clsSimpleObject )
stiThreads(X) = myNewObj
stiThreads(X).P rocessID = X + 1
t(X) = New Threading.Threa d(AddressOf stiThreads(X).E xecute)
X += 1
Loop

For Each thrd As Threading.Threa d In t
thrd.Start()
System.Threadin g.Thread.Sleep( 100)
Next
End Function
End Class

~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~
Public Class inherSimpleObj
Inherits clsSimpleObject
Overrides Sub Execute()
MsgBox(ProcessI D)
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 1244
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.comschre ef in bericht
news:11******** **************@ h44g2000cwa.goo glegroups.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.Thread er
Dim obj As New inherSimpleObj
myThreadRunner. ThreadRunner(10 , obj)
End Sub

~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~
Public Class Threader
Public Function ThreadRunner(By Val nT As Integer, ByVal myProcess As
Object)

Dim myobj As Object
myobj = myProcess

Dim t(nT - 1) As Threading.Threa d
Dim stiThreads(nT - 1) As clsSimpleObject
Dim X As Integer = 0
Do Until X = (nT)
Dim myNewObj As New object
myNewObj = DirectCast(myPr ocess, clsSimpleObject )
stiThreads(X) = myNewObj
stiThreads(X).P rocessID = X + 1
t(X) = New Threading.Threa d(AddressOf stiThreads(X).E xecute)
X += 1
Loop

For Each thrd As Threading.Threa d In t
thrd.Start()
System.Threadin g.Thread.Sleep( 100)
Next
End Function
End Class

~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~
Public Class inherSimpleObj
Inherits clsSimpleObject
Overrides Sub Execute()
MsgBox(ProcessI D)
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
561
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 number of window handle, because if I reduce to about 2 instances of each window, it can run. But not 20 instances of each window. Does anyone know what the problem is? is it really because it exceeds the maximum number of window handle?
21
2516
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 constructors class MyClass { Foo foo; // foo and bar require default constructors Bar bar; public:
4
2397
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 hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
11
4462
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 accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
204
13013
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 = {0,1,2,4,9};
4
7292
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 from an interface and only implementing some of it means something is wrong: either the interface specification is wrong e.g. not minimal or the derivation is wrong e.g. the type can't actually honour this contract.
7
8218
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
5363
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
5
3647
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 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer inside class. Thanks....
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.