System.Reflection.MethodInfo.Invoke and multiple threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Me

    #1

    System.Reflection.MethodInfo.Invoke and multiple threads

    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.



  • Mattias Sjögren

    #2
    Re: System.Reflecti on.MethodInfo.I nvoke and multiple threads

    Hi Me (or should I call you You?),
    [color=blue]
    >Now what rules need to be followed when doing this type of stuff when
    >multiple threads are around.[/color]

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

    [color=blue]
    >Say that Instance is a WinForm and when the method in MethodName is execute
    >it accesses some GUI controls?[/color]

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

    [color=blue]
    >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?[/color]

    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.

    Comment

    • Me

      #3
      Re: System.Reflecti on.MethodInfo.I nvoke and multiple threads

      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" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
      news:eflsCYgHGH A.3100@tk2msftn gp13.phx.gbl...[color=blue]
      > Hi Me (or should I call you You?),
      >[color=green]
      >>Now what rules need to be followed when doing this type of stuff when
      >>multiple threads are around.[/color]
      >
      > The same rules that apply when you invoke the method directly, without
      > going through Reflection. So lets not complicate things.
      >
      >[color=green]
      >>Say that Instance is a WinForm and when the method in MethodName is
      >>execute
      >>it accesses some GUI controls?[/color]
      >
      > That should only be done from the thread the Form was created on.
      >
      >[color=green]
      >>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?[/color]
      >
      > 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.[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: System.Reflecti on.MethodInfo.I nvoke and multiple threads

        Me <me@home.com> wrote:[color=blue]
        > 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?[/color]

        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.
        [color=blue]
        > 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.[/color]

        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 - <skeet@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

        Comment

        Working...