473,396 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 9518
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
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...
6
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...
1
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...
1
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...
0
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...
2
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...
0
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...
9
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...
6
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.