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. Threading question

Threading question

Scheduled Pinned Locked Moved C#
questionsysadminxmltutorial
13 Posts 4 Posters 0 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.
  • S Steve Messer

    I am trying to figure out how to sent an event or set a callback when a thread finishes its work. Lets say that I have 3 functions called: 1) GetData1() // Gets a list of items ( id, name, age ) 2) GetData2() // Gets an item where id = an id from the list from GetData1() ( num, color, etc ) 3) GetData3() // Gets an item where color = color from the list in from GetData2() Each function depends on the results of the prior function in order to be able to do its work. These threads cannot block the main application thread.However, GetData1() may block GetData2() and GetData3() and GetData2() can block GetData3() Each of these worker threads make calls to and XML server, and when a given thread finishes I need to start the next one. The threads can take a long time to complete their task, meaning minutes and not milliseconds etc. How can I know when a previous thread has finsihed? Thanks -- modified at 15:10 Wednesday 14th December, 2005

    W Offline
    W Offline
    WillemM
    wrote on last edited by
    #3

    Sounds rather complex, let me think: Every process should have some kind of semaphore. GetData2 should call wait on the semaphore of GetData1. GetData1 should call signal on the semaphore it has, so that GetData2 continues. GetData3 should call wait on the semaphore of GetData2 and GetData2 should call signal on the semaphore it has, to let GetData3 continue. This should work just fine and you dont have to worry about a lot of the synchronisation here :) There are other ways to do this, but this is by far the simplest solution I can think of. WM.
    What about weapons of mass-construction? -- modified at 15:21 Wednesday 14th December, 2005 (Fixed some minor spelling issues ;P)

    1 Reply Last reply
    0
    • J Judah Gabriel Himango

      You can pass a delegate into the various BeginInvoke methods to have a function execute when the thread is done. For example,

      ThreadStart method1 = GetData1();
      method1.BeginInvoke(myCallbackFunction, null);

      ...

      void MyCallbackFunction(IAsyncResult result)
      {
      // GetData1 is done!
      }

      Does this help? Or am I not understanding your problem?

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango

      W Offline
      W Offline
      WillemM
      wrote on last edited by
      #4

      Hehe, stupid of me this is also a solution. WM.
      What about weapons of mass-construction?

      1 Reply Last reply
      0
      • J Judah Gabriel Himango

        You can pass a delegate into the various BeginInvoke methods to have a function execute when the thread is done. For example,

        ThreadStart method1 = GetData1();
        method1.BeginInvoke(myCallbackFunction, null);

        ...

        void MyCallbackFunction(IAsyncResult result)
        {
        // GetData1 is done!
        }

        Does this help? Or am I not understanding your problem?

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango

        S Offline
        S Offline
        Steve Messer
        wrote on last edited by
        #5

        This seems to be what I am looking for. The snipit you provided doesn't compile, but I can look up the syntax.

        J 1 Reply Last reply
        0
        • S Steve Messer

          This seems to be what I am looking for. The snipit you provided doesn't compile, but I can look up the syntax.

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #6

          It should compile under .NET 2.0 syntax (where delegates can be inferred from the method passed). If you're not using a C# 2.0 compiler, you'll need to explicitly create the delegates:

          ThreadStart method1 = new ThreadStart(GetData1);
          AsyncCallback callback = new AsyncCallback(myCallbackFunction);
          method1.BeginInvoke(callback, null);

          ...

          void MyCallbackFunction(IAsyncResult result)
          {
          // GetData1 is done!
          }

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango

          -- modified at 15:40 Wednesday 14th December, 2005

          S 2 Replies Last reply
          0
          • J Judah Gabriel Himango

            It should compile under .NET 2.0 syntax (where delegates can be inferred from the method passed). If you're not using a C# 2.0 compiler, you'll need to explicitly create the delegates:

            ThreadStart method1 = new ThreadStart(GetData1);
            AsyncCallback callback = new AsyncCallback(myCallbackFunction);
            method1.BeginInvoke(callback, null);

            ...

            void MyCallbackFunction(IAsyncResult result)
            {
            // GetData1 is done!
            }

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango

            -- modified at 15:40 Wednesday 14th December, 2005

            S Offline
            S Offline
            Steve Messer
            wrote on last edited by
            #7

            Thanks that did the trick. Sorry, but I am still in the stone ages using .net 1.1 and Visual Studio 2003 :) Thanks again

            1 Reply Last reply
            0
            • S Steve Messer

              I am trying to figure out how to sent an event or set a callback when a thread finishes its work. Lets say that I have 3 functions called: 1) GetData1() // Gets a list of items ( id, name, age ) 2) GetData2() // Gets an item where id = an id from the list from GetData1() ( num, color, etc ) 3) GetData3() // Gets an item where color = color from the list in from GetData2() Each function depends on the results of the prior function in order to be able to do its work. These threads cannot block the main application thread.However, GetData1() may block GetData2() and GetData3() and GetData2() can block GetData3() Each of these worker threads make calls to and XML server, and when a given thread finishes I need to start the next one. The threads can take a long time to complete their task, meaning minutes and not milliseconds etc. How can I know when a previous thread has finsihed? Thanks -- modified at 15:10 Wednesday 14th December, 2005

              R Offline
              R Offline
              Robert Rohde
              wrote on last edited by
              #8

              Although it seems you already found a solution just another question: If those three threads get executed sequentially (one after the other) why don't you just call all three methods in a single thread?

              S 2 Replies Last reply
              0
              • J Judah Gabriel Himango

                It should compile under .NET 2.0 syntax (where delegates can be inferred from the method passed). If you're not using a C# 2.0 compiler, you'll need to explicitly create the delegates:

                ThreadStart method1 = new ThreadStart(GetData1);
                AsyncCallback callback = new AsyncCallback(myCallbackFunction);
                method1.BeginInvoke(callback, null);

                ...

                void MyCallbackFunction(IAsyncResult result)
                {
                // GetData1 is done!
                }

                Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango

                -- modified at 15:40 Wednesday 14th December, 2005

                S Offline
                S Offline
                Steve Messer
                wrote on last edited by
                #9

                Well, it compiles but it doesn't ever call the callback function Here is my callback function void MyCallbackFunction(IAsyncResult result) { // GetData1 is done!} if( result.IsCompleted ) MessageBox.Show( "Callback triggered"); } Here is how I am calling it ThreadStart method1 = new ThreadStart(RequestVehicles); AsyncCallback callback = new AsyncCallback(MyCallbackFunction); method1.BeginInvoke(callback, null); Thread tThread = new Thread( method1 ); tThread.IsBackground = true; tThread.Start(); Have I done something wrong? -- modified at 17:19 Wednesday 14th December, 2005

                J 1 Reply Last reply
                0
                • R Robert Rohde

                  Although it seems you already found a solution just another question: If those three threads get executed sequentially (one after the other) why don't you just call all three methods in a single thread?

                  S Offline
                  S Offline
                  Steve Messer
                  wrote on last edited by
                  #10

                  Worth a look, thanks

                  1 Reply Last reply
                  0
                  • S Steve Messer

                    Well, it compiles but it doesn't ever call the callback function Here is my callback function void MyCallbackFunction(IAsyncResult result) { // GetData1 is done!} if( result.IsCompleted ) MessageBox.Show( "Callback triggered"); } Here is how I am calling it ThreadStart method1 = new ThreadStart(RequestVehicles); AsyncCallback callback = new AsyncCallback(MyCallbackFunction); method1.BeginInvoke(callback, null); Thread tThread = new Thread( method1 ); tThread.IsBackground = true; tThread.Start(); Have I done something wrong? -- modified at 17:19 Wednesday 14th December, 2005

                    J Offline
                    J Offline
                    Judah Gabriel Himango
                    wrote on last edited by
                    #11

                    In that code snippet, you're calling RequestVehicles 2 times: once when you call method1.BeginInvoke method, and again when you explicitly create your Thread and call .Start on the thread. Just remove that explicit thread creation, it's redundant. Your code should look like:

                    ThreadStart method1 = new ThreadStart(RequestVehicles);
                    AsyncCallback callback = new AsyncCallback(MyCallbackFunction);
                    method1.BeginInvoke(callback, null);

                    void MyCallbackFunction(IAsyncResult result)
                    {
                    // GetData1 is done!}
                    if( result.IsCompleted )
                    MessageBox.Show( "Callback triggered");
                    }

                    Note that if you used

                    Thread t = ...
                    t.Start()

                    it will not call any callback method. Only BeginInvoke does that.

                    S 1 Reply Last reply
                    0
                    • J Judah Gabriel Himango

                      In that code snippet, you're calling RequestVehicles 2 times: once when you call method1.BeginInvoke method, and again when you explicitly create your Thread and call .Start on the thread. Just remove that explicit thread creation, it's redundant. Your code should look like:

                      ThreadStart method1 = new ThreadStart(RequestVehicles);
                      AsyncCallback callback = new AsyncCallback(MyCallbackFunction);
                      method1.BeginInvoke(callback, null);

                      void MyCallbackFunction(IAsyncResult result)
                      {
                      // GetData1 is done!}
                      if( result.IsCompleted )
                      MessageBox.Show( "Callback triggered");
                      }

                      Note that if you used

                      Thread t = ...
                      t.Start()

                      it will not call any callback method. Only BeginInvoke does that.

                      S Offline
                      S Offline
                      Steve Messer
                      wrote on last edited by
                      #12

                      Thanks I will try that directly

                      1 Reply Last reply
                      0
                      • R Robert Rohde

                        Although it seems you already found a solution just another question: If those three threads get executed sequentially (one after the other) why don't you just call all three methods in a single thread?

                        S Offline
                        S Offline
                        Steve Messer
                        wrote on last edited by
                        #13

                        I used a combination of both tips ThreadStart method1 = new ThreadStart(DoRequests); AsyncCallback callback = new AsyncCallback(MyCallbackFunction); method1.BeginInvoke(callback, null); `` void DoRequests() { RequestVehicles(); RequestCustomers(); RequestServiceSales(); } `` void MyCallbackFunction(IAsyncResult result) { if( result.IsCompleted ) { // All data received add to database or whatever } } Thanks All

                        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