Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Asynchronous call of a dynamically loaded method?

Asynchronous call of a dynamically loaded method?

Scheduled Pinned Locked Moved C#
questioncareer
8 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    matthias s 0
    wrote on last edited by
    #1

    Hi, I'd like to invoke a couple of methods retrieved from multiple, dynamically loaded Assemblies (I have the MethodInfo objects) asynchronously and wait for all methods to finish processing before I continue. The method signature is: public void ProcessJob(JobInfo job); Can this be done? If so, how? Thanks in advance. Kind regards

    /matthias

    I love deadlines. I like the whooshing sound they make as they fly by.
    [Douglas Adams]

    N 1 Reply Last reply
    0
    • M matthias s 0

      Hi, I'd like to invoke a couple of methods retrieved from multiple, dynamically loaded Assemblies (I have the MethodInfo objects) asynchronously and wait for all methods to finish processing before I continue. The method signature is: public void ProcessJob(JobInfo job); Can this be done? If so, how? Thanks in advance. Kind regards

      /matthias

      I love deadlines. I like the whooshing sound they make as they fly by.
      [Douglas Adams]

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      matthias s. wrote:

      asynchronously and wait for all methods to finish processing before I continue

      :confused: If you want to wait till all methods finishes, what is the point in using asynchronous calls ?

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      M 1 Reply Last reply
      0
      • N N a v a n e e t h

        matthias s. wrote:

        asynchronously and wait for all methods to finish processing before I continue

        :confused: If you want to wait till all methods finishes, what is the point in using asynchronous calls ?

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

        M Offline
        M Offline
        matthias s 0
        wrote on last edited by
        #3

        they might take quite some time for processing and they should run parallel. once all of them are done, i need to move on.

        /matthias

        I love deadlines. I like the whooshing sound they make as they fly by.
        [Douglas Adams]

        N 1 Reply Last reply
        0
        • M matthias s 0

          they might take quite some time for processing and they should run parallel. once all of them are done, i need to move on.

          /matthias

          I love deadlines. I like the whooshing sound they make as they fly by.
          [Douglas Adams]

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Not tested this, but I believe this almost near to what you need to do. Assume you have MethodInfo array named methods.

          WaitHandle[] waitHandles = new WaitHandle[methods.Length];
          for(int i = 0;i < methods.Length; i++ )
          {
          waitHandles[i] = new AutoResetEvent(false);
          MethodInfo info = methods[i];
          new Thread(delegate(object userState)
          {
          info.Invoke();
          (userState as AutoResetEvent).Set();
          }).Start(waitHandles[i]);
          }
          WaitHandle.WaitAll(waitHandles); // waits for all wait handles to set
          // when control reaches here, all methods will have finished executing

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

          M 1 Reply Last reply
          0
          • N N a v a n e e t h

            Not tested this, but I believe this almost near to what you need to do. Assume you have MethodInfo array named methods.

            WaitHandle[] waitHandles = new WaitHandle[methods.Length];
            for(int i = 0;i < methods.Length; i++ )
            {
            waitHandles[i] = new AutoResetEvent(false);
            MethodInfo info = methods[i];
            new Thread(delegate(object userState)
            {
            info.Invoke();
            (userState as AutoResetEvent).Set();
            }).Start(waitHandles[i]);
            }
            WaitHandle.WaitAll(waitHandles); // waits for all wait handles to set
            // when control reaches here, all methods will have finished executing

            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

            M Offline
            M Offline
            matthias s 0
            wrote on last edited by
            #5

            Thank you very much for your reply. That looks very good and I'll test it as soon as I've understood it. ;) What I don't get is where do I put my parameters (the info object takes a JobInfo parameter) in there and what is the userState object used for? Kind regards

            /matthias

            I love deadlines. I like the whooshing sound they make as they fly by.
            [Douglas Adams]

            N 1 Reply Last reply
            0
            • M matthias s 0

              Thank you very much for your reply. That looks very good and I'll test it as soon as I've understood it. ;) What I don't get is where do I put my parameters (the info object takes a JobInfo parameter) in there and what is the userState object used for? Kind regards

              /matthias

              I love deadlines. I like the whooshing sound they make as they fly by.
              [Douglas Adams]

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              matthias s. wrote:

              where do I put my parameters (the info object takes a JobInfo parameter) in there and what is the userState object used for?

              Well, you need to pass parameter to the Invoke(), right ? You could probably right an inner class and pass this classes instance to the user state, I mean as parameter to the thread's Start() method.

              private class DataCarrier
              {
              JobInfo JobInfoInstance { get; set; }
              WaitHandle Handle { get; set; }

              DataCarrier(JobInfo info,WaitHandle handle){
              this.JobInfoInstance = info;
              this.Handle = handle;
              }
              }

              You can modify the above said method like

              WaitHandle[] waitHandles = new WaitHandle[methods.Length];
              for(int i = 0;i < methods.Length; i++ )
              {
              waitHandles[i] = new AutoResetEvent(false);
              MethodInfo info = methods[i];
              DataCarrier carrier = new DataCarrier(JobInfoInstance,waitHandles[i]);
              new Thread(delegate(object userState)
              {
              DataCarrier c = userState as DataCarrier;
              // use c.JobInfoInstance here

              info.Invoke();
              c.Handle.Set();
              }).Start(carrier);
              }
              WaitHandle.WaitAll(waitHandles); // waits for all wait handles to set
              // when control reaches here, all methods will have finished executing

              All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

              M 1 Reply Last reply
              0
              • N N a v a n e e t h

                matthias s. wrote:

                where do I put my parameters (the info object takes a JobInfo parameter) in there and what is the userState object used for?

                Well, you need to pass parameter to the Invoke(), right ? You could probably right an inner class and pass this classes instance to the user state, I mean as parameter to the thread's Start() method.

                private class DataCarrier
                {
                JobInfo JobInfoInstance { get; set; }
                WaitHandle Handle { get; set; }

                DataCarrier(JobInfo info,WaitHandle handle){
                this.JobInfoInstance = info;
                this.Handle = handle;
                }
                }

                You can modify the above said method like

                WaitHandle[] waitHandles = new WaitHandle[methods.Length];
                for(int i = 0;i < methods.Length; i++ )
                {
                waitHandles[i] = new AutoResetEvent(false);
                MethodInfo info = methods[i];
                DataCarrier carrier = new DataCarrier(JobInfoInstance,waitHandles[i]);
                new Thread(delegate(object userState)
                {
                DataCarrier c = userState as DataCarrier;
                // use c.JobInfoInstance here

                info.Invoke();
                c.Handle.Set();
                }).Start(carrier);
                }
                WaitHandle.WaitAll(waitHandles); // waits for all wait handles to set
                // when control reaches here, all methods will have finished executing

                All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                M Offline
                M Offline
                matthias s 0
                wrote on last edited by
                #7

                Don't get mad at me, but I don't fully understand the lines where the new thread is started.

                N a v a n e e t h wrote:

                new Thread(delegate(object userState) { DataCarrier c = userState as DataCarrier; // use c.JobInfoInstance here info.Invoke(); c.Handle.Set(); }).Start(carrier);

                If I try to compile this in my code, I get the problem that I need to pass a parameter to the info.Invoke() method. Besides that, the WaitHandle doesn't contain a Set() method. Again, thanks for your help and patience.

                /matthias

                I love deadlines. I like the whooshing sound they make as they fly by.
                [Douglas Adams]

                N 1 Reply Last reply
                0
                • M matthias s 0

                  Don't get mad at me, but I don't fully understand the lines where the new thread is started.

                  N a v a n e e t h wrote:

                  new Thread(delegate(object userState) { DataCarrier c = userState as DataCarrier; // use c.JobInfoInstance here info.Invoke(); c.Handle.Set(); }).Start(carrier);

                  If I try to compile this in my code, I get the problem that I need to pass a parameter to the info.Invoke() method. Besides that, the WaitHandle doesn't contain a Set() method. Again, thanks for your help and patience.

                  /matthias

                  I love deadlines. I like the whooshing sound they make as they fly by.
                  [Douglas Adams]

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  matthias s. wrote:

                  WaitHandle doesn't contain a Set() method.

                  Yes. It doesn't contain a set method. It's my mistake, it should be "EventWaitHandle".

                  private class DataCarrier
                  {
                  JobInfo JobInfoInstance { get; set; }
                  EventWaitHandle Handle { get; set; }

                  DataCarrier(JobInfo info,EventWaitHandle handle){
                  this.JobInfoInstance = info;
                  this.Handle = handle;
                  }
                  }

                  matthias s. wrote:

                  but I don't fully understand the lines where the new thread is started.

                  matthias s. wrote:

                  { DataCarrier c = userState as DataCarrier; // use c.JobInfoInstance here info.Invoke(); c.Handle.Set(); }

                  Quoted one is the method which gets executed when thread starts. You can create a new method and put this there if you are confused with anonymous methods. I used it for simplicity.

                  All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups