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.LoadFr om(@AssemblyToR un + ".dll");
Type MyClassToRun = MyAssemblyToRun .GetType(Assemb lyToRun + "." +
ClassToRun, true, true);
MethodInfo MyMethodToRun = MyClassToRun.Ge tMethod(MethodT oRun);
// Create an instance of the class.
object MyClassInstance = Activator.Creat eInstance(MyCla ssToRun);
// Execute the method.
object RetVal = MyMethodToRun.I nvoke(MyClassIn stance, BindingFlags.De fault,
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(Cla ssToRun + "."
MethodToRun));
MyNewThread.Sta rt();
that does not work because "ThreadStar t" 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 3704
kapilp <ka****@discuss ions.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.LoadFr om(@AssemblyToR un + ".dll"); Type MyClassToRun = MyAssemblyToRun .GetType(Assemb lyToRun + "." + ClassToRun, true, true); MethodInfo MyMethodToRun = MyClassToRun.Ge tMethod(MethodT oRun);
// Create an instance of the class. object MyClassInstance = Activator.Creat eInstance(MyCla ssToRun);
// Execute the method. object RetVal = MyMethodToRun.I nvoke(MyClassIn stance, BindingFlags.De fault, 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(Cla ssToRun + "." MethodToRun)); MyNewThread.Sta rt();
that does not work because "ThreadStar t" 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.Create Delegate 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.co m> 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.Create Delegate would help me in this situation. can you explane
further.
thanks for the help.
"Jon Skeet [C# MVP]" wrote: kapilp <ka****@discuss ions.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.LoadFr om(@AssemblyToR un + ".dll"); Type MyClassToRun = MyAssemblyToRun .GetType(Assemb lyToRun + "." + ClassToRun, true, true); MethodInfo MyMethodToRun = MyClassToRun.Ge tMethod(MethodT oRun);
// Create an instance of the class. object MyClassInstance = Activator.Creat eInstance(MyCla ssToRun);
// Execute the method. object RetVal = MyMethodToRun.I nvoke(MyClassIn stance, BindingFlags.De fault, 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(Cla ssToRun + "." MethodToRun)); MyNewThread.Sta rt();
that does not work because "ThreadStar t" 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.Create Delegate 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.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
kapilp <ka****@discuss ions.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(methodI nfo).Run);
i am not sure how creating a ThreadStart delegate using Delegate.Create Delegate would help me in this situation. can you explane further.
You'd use
ThreadStart ts = (ThreadStart) Delegate.Create Delegate
(typeof (ThreadStart), methodInfo);
--
Jon Skeet - <sk***@pobox.co m> 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****@discuss ions.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(methodI nfo).Run);
i am not sure how creating a ThreadStart delegate using Delegate.Create Delegate would help me in this situation. can you explane further.
You'd use
ThreadStart ts = (ThreadStart) Delegate.Create Delegate (typeof (ThreadStart), methodInfo);
-- Jon Skeet - <sk***@pobox.co m> 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****@discuss ions.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(methodI nfo).Run);
i am not sure how creating a ThreadStart delegate using Delegate.Create Delegate would help me in this situation. can you explane further.
You'd use
ThreadStart ts = (ThreadStart) Delegate.Create Delegate (typeof (ThreadStart), methodInfo);
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
kapilp <ka****@discuss ions.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.co m> 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
that has the information, but we as developers have no way to access
this information.
I have come up with a solution for the problem, (as I am sure many
others have) using reflection. Here is some sample code that will
print out the auto...
|
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
that has the information, but we as developers have no way to access
this information.
I have come up with a solution for the problem, (as I am sure many
others have) using reflection. Here is some sample code that will
print out the auto...
|
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 when I got two threads going at the same time.
What I have is two text boxes (textBox1 and textBox2) and four
buttons(cmdStartThread1, cmdStartThread2, cmdStopThread1,
cmdStopThread2)
|
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 instance.
// Declare the types.
string AssemblyToRun = "C:\MyAss";
string ClassToRun = "MyClass"
string MethodToRun = "MyMethod"
| |
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 or more threads are created, the ImportOneFile
method attempts to add a previously added file. If I allow 4 maximum threads
and process 4 files, the last file is attempted 4 times and none of the other
files are added to the destination. If I...
|
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 constructors (because we do not
allow developers to instantiate them directly).
Because our business objects are instantiated very frequently, the idea of
using reflection sounds like a performance killer (I haven't done any tests
on this, but the...
|
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 Exceptional C++, for
example). Platform-specific (e.g.: Win32, POSIX) is OK, as long as it's
good :)
Thank you,
Ray
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |