using Reflection with Multithreading

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

    #1

    using Reflection with Multithreading

    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"

    // Setup reflection.
    Assembly MyAssemblyToRun = Assembly.LoadFr om(@AssemblyToR un + ".dll");
    Type MyClassToRun = MyAssemblyToRun .GetType(Assemb lyToRun + "." +
    ClassToRun, true, true);
    MethodInfo MyMethodToRun = MyClassToRun.Ge tMethod(MethodT oRun);

    // Create an instance of the class.
    object MyClassInstance = Activator.Creat eInstance(MyCla ssToRun);

    // Execute the method.
    object RetVal = MyMethodToRun.I nvoke(MyClassIn stance, BindingFlags.De fault,
    null, null, null);

    simple right. ok. all the above works fine. now if i want to execute the
    method on a new thread I would add something like:

    Thread MyNewThread = new Thread(new ThreadStart(Cla ssToRun + "."
    MethodToRun));
    MyNewThread.Sta rt();

    that does not work because "ThreadStar t" is a delegate that wants a "void"
    method and all i can pass it is a class instance or the method name as a
    string. it also wont work when i use the class instance or the MyMethodToRun
    instance of the reflected method.

    how can i use reflection and multithreading together? is this possibble? is
    there a way to invoke a reflected method on a new thread?

    HELP !!!
  • Jon Skeet [C# MVP]

    #2
    Re: using Reflection with Multithreading

    kapilp <kapilp@discuss ions.microsoft. com> wrote:[color=blue]
    > 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"
    >
    > // Setup reflection.
    > Assembly MyAssemblyToRun = Assembly.LoadFr om(@AssemblyToR un + ".dll");
    > Type MyClassToRun = MyAssemblyToRun .GetType(Assemb lyToRun + "." +
    > ClassToRun, true, true);
    > MethodInfo MyMethodToRun = MyClassToRun.Ge tMethod(MethodT oRun);
    >
    > // Create an instance of the class.
    > object MyClassInstance = Activator.Creat eInstance(MyCla ssToRun);
    >
    > // Execute the method.
    > object RetVal = MyMethodToRun.I nvoke(MyClassIn stance, BindingFlags.De fault,
    > null, null, null);
    >
    > simple right. ok. all the above works fine. now if i want to execute the
    > method on a new thread I would add something like:
    >
    > Thread MyNewThread = new Thread(new ThreadStart(Cla ssToRun + "."
    > MethodToRun));
    > MyNewThread.Sta rt();
    >
    > that does not work because "ThreadStar t" is a delegate that wants a "void"
    > method and all i can pass it is a class instance or the method name as a
    > string. it also wont work when i use the class instance or the MyMethodToRun
    > instance of the reflected method.
    >
    > how can i use reflection and multithreading together? is this possibble? is
    > there a way to invoke a reflected method on a new thread?[/color]

    Well, you can use Delegate.Create Delegate to create a ThreadStart
    delegate. Alternatively, just create an instance of a class which knows
    which MethodInfo to call, and provides a ThreadStart-compatible method
    which can be used as the entry point for the thread, and just calls the
    method.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • kapilp

      #3
      Re: using Reflection with Multithreading

      I cant do the latter because this is a data driven job scheduler program.
      This program looks for code to execute on an interval basis (based on the
      data in the sql server), hourly, or daily etc. then it goes and executes the
      code in the correct assembly for that job.

      so since its data driven i cant create a seperate method for each job
      (method).

      i am not sure how creating a ThreadStart delegate using
      Delegate.Create Delegate would help me in this situation. can you explane
      further.

      thanks for the help.

      "Jon Skeet [C# MVP]" wrote:
      [color=blue]
      > kapilp <kapilp@discuss ions.microsoft. com> wrote:[color=green]
      > > 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"
      > >
      > > // Setup reflection.
      > > Assembly MyAssemblyToRun = Assembly.LoadFr om(@AssemblyToR un + ".dll");
      > > Type MyClassToRun = MyAssemblyToRun .GetType(Assemb lyToRun + "." +
      > > ClassToRun, true, true);
      > > MethodInfo MyMethodToRun = MyClassToRun.Ge tMethod(MethodT oRun);
      > >
      > > // Create an instance of the class.
      > > object MyClassInstance = Activator.Creat eInstance(MyCla ssToRun);
      > >
      > > // Execute the method.
      > > object RetVal = MyMethodToRun.I nvoke(MyClassIn stance, BindingFlags.De fault,
      > > null, null, null);
      > >
      > > simple right. ok. all the above works fine. now if i want to execute the
      > > method on a new thread I would add something like:
      > >
      > > Thread MyNewThread = new Thread(new ThreadStart(Cla ssToRun + "."
      > > MethodToRun));
      > > MyNewThread.Sta rt();
      > >
      > > that does not work because "ThreadStar t" is a delegate that wants a "void"
      > > method and all i can pass it is a class instance or the method name as a
      > > string. it also wont work when i use the class instance or the MyMethodToRun
      > > instance of the reflected method.
      > >
      > > how can i use reflection and multithreading together? is this possibble? is
      > > there a way to invoke a reflected method on a new thread?[/color]
      >
      > Well, you can use Delegate.Create Delegate to create a ThreadStart
      > delegate. Alternatively, just create an instance of a class which knows
      > which MethodInfo to call, and provides a ThreadStart-compatible method
      > which can be used as the entry point for the thread, and just calls the
      > method.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too
      >[/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: using Reflection with Multithreading

        kapilp <kapilp@discuss ions.microsoft. com> wrote:[color=blue]
        > I cant do the latter because this is a data driven job scheduler program.
        > This program looks for code to execute on an interval basis (based on the
        > data in the sql server), hourly, or daily etc. then it goes and executes the
        > code in the correct assembly for that job.
        >
        > so since its data driven i cant create a seperate method for each job
        > (method).[/color]

        You don't need to. You write a class which accepts a MethodInfo, and
        invokes that MethodInfo appropriately. Something like (untested):

        public class Invoker
        {
        MethodInfo method;

        public Invoker (MethodInfo method)
        {
        this.method = method;
        }

        public void Run()
        {
        method.Invoke (null, null);
        }
        }

        You'd then do:

        ThreadStart ts = new ThreadStart(new Invoker(methodI nfo).Run);
        [color=blue]
        > i am not sure how creating a ThreadStart delegate using
        > Delegate.Create Delegate would help me in this situation. can you explane
        > further.[/color]

        You'd use

        ThreadStart ts = (ThreadStart) Delegate.Create Delegate
        (typeof (ThreadStart), methodInfo);

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • kapilp

          #5
          Re: using Reflection with Multithreading

          when i create the delegate i get the following error.

          "Error binding to target method."

          any ideas.

          i am going to try the other method now.

          "Jon Skeet [C# MVP]" wrote:
          [color=blue]
          > kapilp <kapilp@discuss ions.microsoft. com> wrote:[color=green]
          > > I cant do the latter because this is a data driven job scheduler program.
          > > This program looks for code to execute on an interval basis (based on the
          > > data in the sql server), hourly, or daily etc. then it goes and executes the
          > > code in the correct assembly for that job.
          > >
          > > so since its data driven i cant create a seperate method for each job
          > > (method).[/color]
          >
          > You don't need to. You write a class which accepts a MethodInfo, and
          > invokes that MethodInfo appropriately. Something like (untested):
          >
          > public class Invoker
          > {
          > MethodInfo method;
          >
          > public Invoker (MethodInfo method)
          > {
          > this.method = method;
          > }
          >
          > public void Run()
          > {
          > method.Invoke (null, null);
          > }
          > }
          >
          > You'd then do:
          >
          > ThreadStart ts = new ThreadStart(new Invoker(methodI nfo).Run);
          >[color=green]
          > > i am not sure how creating a ThreadStart delegate using
          > > Delegate.Create Delegate would help me in this situation. can you explane
          > > further.[/color]
          >
          > You'd use
          >
          > ThreadStart ts = (ThreadStart) Delegate.Create Delegate
          > (typeof (ThreadStart), methodInfo);
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too
          >[/color]

          Comment

          • kapilp

            #6
            Re: using Reflection with Multithreading

            ok, i created the class and when i step through it it works and I can do
            MyThread.Start( )
            but as soon as the control returns to the front end I get this error
            {"Non-static method requires a target."}
            in the Invoker.Run() method like its telling me to create an instance of the
            class first but i already did that via reflection is the calling method.

            "Jon Skeet [C# MVP]" wrote:
            [color=blue]
            > kapilp <kapilp@discuss ions.microsoft. com> wrote:[color=green]
            > > I cant do the latter because this is a data driven job scheduler program.
            > > This program looks for code to execute on an interval basis (based on the
            > > data in the sql server), hourly, or daily etc. then it goes and executes the
            > > code in the correct assembly for that job.
            > >
            > > so since its data driven i cant create a seperate method for each job
            > > (method).[/color]
            >
            > You don't need to. You write a class which accepts a MethodInfo, and
            > invokes that MethodInfo appropriately. Something like (untested):
            >
            > public class Invoker
            > {
            > MethodInfo method;
            >
            > public Invoker (MethodInfo method)
            > {
            > this.method = method;
            > }
            >
            > public void Run()
            > {
            > method.Invoke (null, null);
            > }
            > }
            >
            > You'd then do:
            >
            > ThreadStart ts = new ThreadStart(new Invoker(methodI nfo).Run);
            >[color=green]
            > > i am not sure how creating a ThreadStart delegate using
            > > Delegate.Create Delegate would help me in this situation. can you explane
            > > further.[/color]
            >
            > You'd use
            >
            > ThreadStart ts = (ThreadStart) Delegate.Create Delegate
            > (typeof (ThreadStart), methodInfo);
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too
            >[/color]

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: using Reflection with Multithreading

              kapilp <kapilp@discuss ions.microsoft. com> wrote:[color=blue]
              > ok, i created the class and when i step through it it works and I can do
              > MyThread.Start( )
              > but as soon as the control returns to the front end I get this error
              > {"Non-static method requires a target."}
              > in the Invoker.Run() method like its telling me to create an instance of the
              > class first but i already did that via reflection is the calling method.[/color]

              Then you need to pass that instance to the Invoker as well, and use it
              in the call to method.Invoke.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              Working...