Passing Object and Type to Thread Starter

  • Thread starter Thread starter WildHare
  • Start date Start date
W

WildHare

I have a generalized CreateThread method which is passed an object (e.g.
CreateThread( object objIn ) ). The object that I am passing are
instantiations of one of several different classes all of which contain an
execute method ( execute() ). We don't know until the CreateThread method
is called which type of object will be passed.

Inside the CreateThread method we get to the usual:

Thread cThread = new Thread( new ThreadStart( objIn.execute ));

Which, of course doesn't work because something of type "object" (the type
indicated in the parameter list) does not contain the execute() method.

I can cast objIn by writing:

Thread cThread = new Thread( new ThreadStart( (ClassName)objIn.execute ));
// and this works

but that suggests that I know what ClassName is when I write the
code...which I don't.

I have tried several variants of objIn.GetType(), etc...and I can't make it
work. I suspect it is something like that, but I can't get it.

Any suggestions are welcome.
 
How about an interface with that execute method...

u can declare objIn as that interface type, and then u can just cast it to any type which implements that interface...

not sure whether this works for your situation, but just thought to tell you...

Regards,
NetPointer
 
WildHare said:
I have a generalized CreateThread method which is passed an object (e.g.
CreateThread( object objIn ) ). The object that I am passing are
instantiations of one of several different classes all of which contain an
execute method ( execute() ). We don't know until the CreateThread method
is called which type of object will be passed.

Inside the CreateThread method we get to the usual:

Thread cThread = new Thread( new ThreadStart( objIn.execute ));

Which, of course doesn't work because something of type "object" (the type
indicated in the parameter list) does not contain the execute() method.

I can cast objIn by writing:

Thread cThread = new Thread( new ThreadStart( (ClassName)objIn.execute ));
// and this works

but that suggests that I know what ClassName is when I write the
code...which I don't.

I have tried several variants of objIn.GetType(), etc...and I can't make it
work. I suspect it is something like that, but I can't get it.

Any suggestions are welcome.

Either define an interface that contains the Execute method, or use
reflection.

Here's how to do it with an interface.

using System;
using System.Threading;

interface IExecutable
{
void Execute();
}

class foo : IExecutable
{
public void Execute()
{
Console.WriteLine("Foo Executing");
}

}
class Class1
{

static void Main(string[] args)
{
foo f = new foo();
Thread t = CreateThread(f);
t.Join();
}

static Thread CreateThread( IExecutable objIn )
{
Thread t = new Thread( new ThreadStart(objIn.Execute));
t.Start();
return t;
}


}


David
 
Thanks! Perfect. Works great!

K


David Browne said:
WildHare said:
I have a generalized CreateThread method which is passed an object (e.g.
CreateThread( object objIn ) ). The object that I am passing are
instantiations of one of several different classes all of which contain an
execute method ( execute() ). We don't know until the CreateThread method
is called which type of object will be passed.

Inside the CreateThread method we get to the usual:

Thread cThread = new Thread( new ThreadStart( objIn.execute ));

Which, of course doesn't work because something of type "object" (the type
indicated in the parameter list) does not contain the execute() method.

I can cast objIn by writing:

Thread cThread = new Thread( new ThreadStart( (ClassName)objIn.execute ));
// and this works

but that suggests that I know what ClassName is when I write the
code...which I don't.

I have tried several variants of objIn.GetType(), etc...and I can't make it
work. I suspect it is something like that, but I can't get it.

Any suggestions are welcome.

Either define an interface that contains the Execute method, or use
reflection.

Here's how to do it with an interface.

using System;
using System.Threading;

interface IExecutable
{
void Execute();
}

class foo : IExecutable
{
public void Execute()
{
Console.WriteLine("Foo Executing");
}

}
class Class1
{

static void Main(string[] args)
{
foo f = new foo();
Thread t = CreateThread(f);
t.Join();
}

static Thread CreateThread( IExecutable objIn )
{
Thread t = new Thread( new ThreadStart(objIn.Execute));
t.Start();
return t;
}


}


David
 
Back
Top