471,337 Members | 1,162 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

System.Reflection.MethodInfo.Invoke and multiple threads

Me
I am trying to figure out any issues with calling
System.Reflection.MethodInfo.Invoke() 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.Reflection.MethodInfo Info;

Info = Instance.GetType().GetMethod(MethodName);
Info.Invoke(Instance, 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 9332
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.Invoke 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.Timer 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.org> wrote in message
news:ef**************@tk2msftngp13.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.Invoke 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.Timer 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.Invoke 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.com>
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Derek Hart | last post: by
6 posts views Thread by kapilp | last post: by
2 posts views Thread by Fredrik Strandberg | last post: by
6 posts views Thread by Cralis | last post: by

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.