By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,183 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,183 IT Pros & Developers. It's quick & easy.

using Reflection with Multithreading

P: n/a
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 !!!
Jul 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
kapilp <ka****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

P: n/a
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.

"Jon Skeet [C# MVP]" wrote:
kapilp <ka****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3

P: n/a
kapilp <ka****@discussions.microsoft.com> wrote:
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);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

P: n/a
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.

"Jon Skeet [C# MVP]" wrote:
kapilp <ka****@discussions.microsoft.com> wrote:
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);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #5

P: n/a
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.

"Jon Skeet [C# MVP]" wrote:
kapilp <ka****@discussions.microsoft.com> wrote:
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);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #6

P: n/a
kapilp <ka****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.