Dynamic creation of worker threads

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

Hi,

My project will have a number of classes all implementing a single
interface, e.g.

class A : IA
class B : IA
class C : IA
(etc)

Given a list of class names (from a config file, e.g. a string[] = {"A",
"B", "C"}), how could I dynamically start each one as a separate thread?

Thanks!!
Alex
 
Alex said:
My project will have a number of classes all implementing a single
interface, e.g.

class A : IA
class B : IA
class C : IA
(etc)

Given a list of class names (from a config file, e.g. a string[] = {"A",
"B", "C"}), how could I dynamically start each one as a separate thread?

Well, you can't "start" a class, but you could fairly easily run a
method specified in IA in a separate thread:

1) Use reflection to create the instances.
2) For each instance, create a new ThreadStart delegate targetting an
IA method acting on the instance
3) For each of the delegates, create a new thread and start it.

I suspect you're stuck on one of those steps, but it's hard to say
which - if you could elaborate, I'm sure we can help further.

Jon
 
Alex said:
My project will have a number of classes all implementing a single
interface, e.g.

class A : IA
class B : IA
class C : IA
(etc)

Given a list of class names (from a config file, e.g. a string[] = {"A",
"B", "C"}), how could I dynamically start each one as a separate thread?

Well, you can't "start" a class, but you could fairly easily run a
method specified in IA in a separate thread:

1) Use reflection to create the instances.
2) For each instance, create a new ThreadStart delegate targetting an
IA method acting on the instance
3) For each of the delegates, create a new thread and start it.

I suspect you're stuck on one of those steps, but it's hard to say
which - if you could elaborate, I'm sure we can help further.

Jon
 
Jon,

Thanks for your prompt reply. I should have added that one of the interface
methods is called "start" - so I guess I was describing it in my terms...
that would be the method to call when

I'm able to create the instances using reflection, it's the threading that's
confusing me. Are there any samples/documentation I could look at to figure
this out?

Thanks,
Alex


Jon Skeet said:
Alex said:
My project will have a number of classes all implementing a single
interface, e.g.

class A : IA
class B : IA
class C : IA
(etc)

Given a list of class names (from a config file, e.g. a string[] = {"A",
"B", "C"}), how could I dynamically start each one as a separate thread?

Well, you can't "start" a class, but you could fairly easily run a
method specified in IA in a separate thread:

1) Use reflection to create the instances.
2) For each instance, create a new ThreadStart delegate targetting an
IA method acting on the instance
3) For each of the delegates, create a new thread and start it.

I suspect you're stuck on one of those steps, but it's hard to say
which - if you could elaborate, I'm sure we can help further.

Jon
 
If you have the object than you can call:

ThreadStart ts = new ThreadStart(iaObject.start);
Threat t = new Thread(ts);
t.Start();

This will start the start method of your object on a seperate thread. Is
that what you had in mind?

saso

Alex said:
Jon,

Thanks for your prompt reply. I should have added that one of the
interface methods is called "start" - so I guess I was describing it in my
terms... that would be the method to call when

I'm able to create the instances using reflection, it's the threading
that's confusing me. Are there any samples/documentation I could look at
to figure this out?

Thanks,
Alex


Jon Skeet said:
Alex said:
My project will have a number of classes all implementing a single
interface, e.g.

class A : IA
class B : IA
class C : IA
(etc)

Given a list of class names (from a config file, e.g. a string[] = {"A",
"B", "C"}), how could I dynamically start each one as a separate thread?

Well, you can't "start" a class, but you could fairly easily run a
method specified in IA in a separate thread:

1) Use reflection to create the instances.
2) For each instance, create a new ThreadStart delegate targetting an
IA method acting on the instance
3) For each of the delegates, create a new thread and start it.

I suspect you're stuck on one of those steps, but it's hard to say
which - if you could elaborate, I'm sure we can help further.

Jon
 
Back
Top