473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

using Reflection with Multithreading

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
6 3677
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
0
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
6
by: kapilp | last post by:
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...
5
by: mrkbrndck | last post by:
Please see the code below as I am trying to use multithreading for copying files to a new location in a way that improves performance of the client windows application. The problem occurs when 2...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
11
by: GVN | last post by:
Hi All, Can anyone guide me when asynchronous method calls will be benificial? Are there any disadvantages of using asynchronous calls? Thanks,
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.