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 !!! 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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
|
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...
| |