using Reflection with Multithreading

G

Guest

hi,

i have a program that used reflection to execute methods. now i want to
execute the reflected method on a new thread but cant figure out how or if it
can be done. take the below code, for instance.

// Declare the types.
string AssemblyToRun = "C:\MyAss";
string ClassToRun = "MyClass"
string MethodToRun = "MyMethod"

// Setup reflection.
Assembly MyAssemblyToRun = Assembly.LoadFrom(@AssemblyToRun + ".dll");
Type MyClassToRun = MyAssemblyToRun.GetType(AssemblyToRun + "." +
ClassToRun, true, true);
MethodInfo MyMethodToRun = MyClassToRun.GetMethod(MethodToRun);

// Create an instance of the class.
object MyClassInstance = Activator.CreateInstance(MyClassToRun);

// Execute the method.
object RetVal = MyMethodToRun.Invoke(MyClassInstance, BindingFlags.Default,
null, null, null);

simple right. ok. all the above works fine. now if i want to execute the
method on a new thread I would add something like:

Thread MyNewThread = new Thread(new ThreadStart(ClassToRun + "."
MethodToRun));
MyNewThread.Start();

that does not work because "ThreadStart" is a delegate that wants a "void"
method and all i can pass it is a class instance or the method name as a
string. it also wont work when i use the class instance or the MyMethodToRun
instance of the reflected method.

how can i use reflection and multithreading together? is this possibble? is
there a way to invoke a reflected method on a new thread?

HELP !!!
 
J

Jon Skeet [C# MVP]

kapilp said:
i have a program that used reflection to execute methods. now i want to
execute the reflected method on a new thread but cant figure out how or if it
can be done. take the below code, for instance.

// Declare the types.
string AssemblyToRun = "C:\MyAss";
string ClassToRun = "MyClass"
string MethodToRun = "MyMethod"

// Setup reflection.
Assembly MyAssemblyToRun = Assembly.LoadFrom(@AssemblyToRun + ".dll");
Type MyClassToRun = MyAssemblyToRun.GetType(AssemblyToRun + "." +
ClassToRun, true, true);
MethodInfo MyMethodToRun = MyClassToRun.GetMethod(MethodToRun);

// Create an instance of the class.
object MyClassInstance = Activator.CreateInstance(MyClassToRun);

// Execute the method.
object RetVal = MyMethodToRun.Invoke(MyClassInstance, BindingFlags.Default,
null, null, null);

simple right. ok. all the above works fine. now if i want to execute the
method on a new thread I would add something like:

Thread MyNewThread = new Thread(new ThreadStart(ClassToRun + "."
MethodToRun));
MyNewThread.Start();

that does not work because "ThreadStart" is a delegate that wants a "void"
method and all i can pass it is a class instance or the method name as a
string. it also wont work when i use the class instance or the MyMethodToRun
instance of the reflected method.

how can i use reflection and multithreading together? is this possibble? is
there a way to invoke a reflected method on a new thread?

Well, you can use Delegate.CreateDelegate to create a ThreadStart
delegate. Alternatively, just create an instance of a class which knows
which MethodInfo to call, and provides a ThreadStart-compatible method
which can be used as the entry point for the thread, and just calls the
method.
 
G

Guest

I cant do the latter because this is a data driven job scheduler program.
This program looks for code to execute on an interval basis (based on the
data in the sql server), hourly, or daily etc. then it goes and executes the
code in the correct assembly for that job.

so since its data driven i cant create a seperate method for each job
(method).

i am not sure how creating a ThreadStart delegate using
Delegate.CreateDelegate would help me in this situation. can you explane
further.

thanks for the help.
 
J

Jon Skeet [C# MVP]

kapilp said:
I cant do the latter because this is a data driven job scheduler program.
This program looks for code to execute on an interval basis (based on the
data in the sql server), hourly, or daily etc. then it goes and executes the
code in the correct assembly for that job.

so since its data driven i cant create a seperate method for each job
(method).

You don't need to. You write a class which accepts a MethodInfo, and
invokes that MethodInfo appropriately. Something like (untested):

public class Invoker
{
MethodInfo method;

public Invoker (MethodInfo method)
{
this.method = method;
}

public void Run()
{
method.Invoke (null, null);
}
}

You'd then do:

ThreadStart ts = new ThreadStart(new Invoker(methodInfo).Run);
i am not sure how creating a ThreadStart delegate using
Delegate.CreateDelegate would help me in this situation. can you explane
further.

You'd use

ThreadStart ts = (ThreadStart) Delegate.CreateDelegate
(typeof (ThreadStart), methodInfo);
 
G

Guest

when i create the delegate i get the following error.

"Error binding to target method."

any ideas.

i am going to try the other method now.
 
G

Guest

ok, i created the class and when i step through it it works and I can do
MyThread.Start()
but as soon as the control returns to the front end I get this error
{"Non-static method requires a target."}
in the Invoker.Run() method like its telling me to create an instance of the
class first but i already did that via reflection is the calling method.
 
J

Jon Skeet [C# MVP]

kapilp said:
ok, i created the class and when i step through it it works and I can do
MyThread.Start()
but as soon as the control returns to the front end I get this error
{"Non-static method requires a target."}
in the Invoker.Run() method like its telling me to create an instance of the
class first but i already did that via reflection is the calling method.

Then you need to pass that instance to the Invoker as well, and use it
in the call to method.Invoke.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top