System.Reflection.MethodInfo.Invoke and multiple threads

M

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.
 
M

Mattias Sjögren

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
 
M

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.
 
J

Jon Skeet [C# MVP]

Me said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top