473,545 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Reflecti on.MethodInfo.I nvoke and multiple threads

Me
I am trying to figure out any issues with calling
System.Reflecti on.MethodInfo.I nvoke() when dealing with multiple threads.

For instance..

Say I have a class that allows you to pass in a string that represents a
method name and an object that represents an instance of some class which
contains the previous function name. Now, I want to execute that method name
by calling the Invoke() method of MethodInfo similar to the following
example.

object Instance; //Represents the object that has the method to execute.
String MethodName; //The method to execute.
System.Reflecti on.MethodInfo Info;

Info = Instance.GetTyp e().GetMethod(M ethodName);
Info.Invoke(Ins tance, null);

Now what rules need to be followed when doing this type of stuff when
multiple threads are around.

Say that Instance is a WinForm and when the method in MethodName is execute
it accesses some GUI controls?

I have heard mention of BeginInvoke() before or something like that for
executing methods that are in different threads.. Does the Invoke() method
used above work in that same manner?

Any thoughts/suggestions would be appreciated.

Jan 20 '06 #1
3 9553
Hi Me (or should I call you You?),
Now what rules need to be followed when doing this type of stuff when
multiple threads are around.
The same rules that apply when you invoke the method directly, without
going through Reflection. So lets not complicate things.

Say that Instance is a WinForm and when the method in MethodName is execute
it accesses some GUI controls?
That should only be done from the thread the Form was created on.

I have heard mention of BeginInvoke() before or something like that for
executing methods that are in different threads.. Does the Invoke() method
used above work in that same manner?


No MethodInfo.Invo ke is not a substitute for Control.Invoke if that's
what you're asking.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 20 '06 #2
Me
Ok..

Say I have a collection of MethodNames and Instances that need to be
executed. If I create a System.Timers.T imer and have it perform the Invoke
method as described before what do I need to worry about? Should I not be
using Invoke()? If not then how can I do this safely?

I have no knowledge ahead of tie about the Instance itself - it could be a
WinForm class, generic class, who knows what! It is publisher/subscriber
type of idea so anyone could subscribe to an event.

Thanks.
"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:ef******** ******@tk2msftn gp13.phx.gbl...
Hi Me (or should I call you You?),
Now what rules need to be followed when doing this type of stuff when
multiple threads are around.


The same rules that apply when you invoke the method directly, without
going through Reflection. So lets not complicate things.

Say that Instance is a WinForm and when the method in MethodName is
execute
it accesses some GUI controls?


That should only be done from the thread the Form was created on.

I have heard mention of BeginInvoke() before or something like that for
executing methods that are in different threads.. Does the Invoke() method
used above work in that same manner?


No MethodInfo.Invo ke is not a substitute for Control.Invoke if that's
what you're asking.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jan 20 '06 #3
Me <me@home.com> wrote:
Say I have a collection of MethodNames and Instances that need to be
executed. If I create a System.Timers.T imer and have it perform the Invoke
method as described before what do I need to worry about? Should I not be
using Invoke()? If not then how can I do this safely?
You'd need to call Control.Invoke to get onto the UI thread, then call
MethodInfo.Invo ke from there, if you wanted to run the methods on the
UI thread.
I have no knowledge ahead of tie about the Instance itself - it could be a
WinForm class, generic class, who knows what! It is publisher/subscriber
type of idea so anyone could subscribe to an event.


Then perhaps you'd be better off publishing that the event isn't
guaranteed to be called on the UI thread, and that subscribers should
make sure they can be called from any thread.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3675
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can...
6
3700
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"
1
2599
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am experimenting a bit with what I need to do this and have the following code snippet. But first if I pass the assembly name and type to...
1
6616
by: melanieab | last post by:
Hi again, I'm trying to programatically sort a datagrid. I did find the following code, and it does work, but, when a column header is clicked, the data only sorts in descending order. Clicking the same column header again doesn't change anything, although clicking a different column header re-sorts the data in descending order according to...
0
1539
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to execute button2's click event. This works great when i know the name of the method that i want to invoke. I do so by doing this: Dim...
2
2063
by: Fredrik Strandberg | last post by:
I have not been able to find the solution of this problem anywhere: I am building a class PrivateHelper that provides methods to access private members and invoke private methods, to be used for testing. In a particular test case, an array is passed to the Method Under Test, and then that MUT performs a ReDim on the array. The test code...
0
1401
by: pso19 | last post by:
Hi, I am trying to access all the properties, methods of an assembly using reflection. But what i want to do is invoke a method which contains custom parametrs like enumeration defined in a different assembly. For ex: Dim assemblyToLoad As Assembly = Nothing assemblyToLoad = Assembly.LoadFrom(AssemblyName)
9
4154
by: Michael Sander | last post by:
Hello ng, anyone knowns how to add a reference to an assembly to System.Reflection.AssemblyBuilder? In System.Web.Compilation.AssemblyBuilder is a function like AddAssemblyReference, but not in System.Reflection. Regards, Michael
6
1959
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept quiet and smiled. What is reflection!? I have been reading it up, and all I can work out is that it has something to do with Late Binding, and...
0
7409
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7664
Oralloy
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. ...
0
7921
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5982
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
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...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1900
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
0
720
bsmnconsultancy
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...

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.