473,396 Members | 1,799 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,396 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 3684
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.