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. Waiting for other threads

Waiting for other threads

Scheduled Pinned Locked Moved C#
question
10 Posts 6 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.
  • R Offline
    R Offline
    Roger Alsing 0
    wrote on last edited by
    #1

    If I fire up a few threads and then want to stop execution in my main thread untill those threads are finished. What is the most correct way to wait for those threads? I've always been cheeting and use a dumb loop which checks some condition and then sleeps x ms if the condition was not met. But I guess thats far from optimal. (Although thats what I read that lock{}/monitor.tryenter does internally.) pseudo sample of what I do: void Foo() { perform some async tasks wait untill all of them are done // whats the best way to achive this part? return }

    L G B 3 Replies Last reply
    0
    • R Roger Alsing 0

      If I fire up a few threads and then want to stop execution in my main thread untill those threads are finished. What is the most correct way to wait for those threads? I've always been cheeting and use a dumb loop which checks some condition and then sleeps x ms if the condition was not met. But I guess thats far from optimal. (Although thats what I read that lock{}/monitor.tryenter does internally.) pseudo sample of what I do: void Foo() { perform some async tasks wait untill all of them are done // whats the best way to achive this part? return }

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      Roger Alsing, You could use the AsyncCallback. http://msdn.microsoft.com/en-us/library/system.asynccallback.aspx[^] Regards, Gareth.

      R 1 Reply Last reply
      0
      • R Roger Alsing 0

        If I fire up a few threads and then want to stop execution in my main thread untill those threads are finished. What is the most correct way to wait for those threads? I've always been cheeting and use a dumb loop which checks some condition and then sleeps x ms if the condition was not met. But I guess thats far from optimal. (Although thats what I read that lock{}/monitor.tryenter does internally.) pseudo sample of what I do: void Foo() { perform some async tasks wait untill all of them are done // whats the best way to achive this part? return }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You could call the Thread.Join[^] method on each thread you start, this will block the calling thread until all threads are finished. regards

        1 Reply Last reply
        0
        • R Roger Alsing 0

          If I fire up a few threads and then want to stop execution in my main thread untill those threads are finished. What is the most correct way to wait for those threads? I've always been cheeting and use a dumb loop which checks some condition and then sleeps x ms if the condition was not met. But I guess thats far from optimal. (Although thats what I read that lock{}/monitor.tryenter does internally.) pseudo sample of what I do: void Foo() { perform some async tasks wait untill all of them are done // whats the best way to achive this part? return }

          B Offline
          B Offline
          Broken Bokken
          wrote on last edited by
          #4

          My standard model for multithreading (in a windows app) is to create a a wait form. You can put a progress bar or a label or whatever you want to use. For my threads I use the System.ComponentModel.BackgroundWorker. I'll briefely explain the basics. My code will create the BackgroundWorker and set up the method it will need to run.

          BackgroundWorker myWorker = new BackgroundWorker();
          myWorker.DoWork += new DoWorkEventHandler(myWorker_DoWork);

          The method passed in looks like this

          private void myWorker_DoWork(object sender, DoWorkEventArgs e)
          {
          //...
          }

          My WaitForm accepts the BackgroundWorker as a parameter to the constructor. My WaitForm accepts the background worker as an input parameter. You can set up the worker in the WaitForm to handle progress changed events to modify the message and even a progress bar in the wait form. Also, you will want to set the RunWorkerCompleted event and make it close the WaitForm. Then, append this below the first block of code.

          WaitForm waiting = new WaitForm(myWorker);
          waiting.ShowDialog();

          By using ShowDialog you are forcing the current thread to wait until the dialog is done. Because the worker is working the dialog won't close until myWorker_DoWork completes. Also, this is another good approach which I have used on a few applications.

          Broken Bokken You can't carry out a ninja-style assasination dressed as an astronaut. It's the luminous fabric; too visible. - Tripod The story of your fighting is a poem of two words: YOU SUCK.

          R 1 Reply Last reply
          0
          • B Broken Bokken

            My standard model for multithreading (in a windows app) is to create a a wait form. You can put a progress bar or a label or whatever you want to use. For my threads I use the System.ComponentModel.BackgroundWorker. I'll briefely explain the basics. My code will create the BackgroundWorker and set up the method it will need to run.

            BackgroundWorker myWorker = new BackgroundWorker();
            myWorker.DoWork += new DoWorkEventHandler(myWorker_DoWork);

            The method passed in looks like this

            private void myWorker_DoWork(object sender, DoWorkEventArgs e)
            {
            //...
            }

            My WaitForm accepts the BackgroundWorker as a parameter to the constructor. My WaitForm accepts the background worker as an input parameter. You can set up the worker in the WaitForm to handle progress changed events to modify the message and even a progress bar in the wait form. Also, you will want to set the RunWorkerCompleted event and make it close the WaitForm. Then, append this below the first block of code.

            WaitForm waiting = new WaitForm(myWorker);
            waiting.ShowDialog();

            By using ShowDialog you are forcing the current thread to wait until the dialog is done. Because the worker is working the dialog won't close until myWorker_DoWork completes. Also, this is another good approach which I have used on a few applications.

            Broken Bokken You can't carry out a ninja-style assasination dressed as an astronaut. It's the luminous fabric; too visible. - Tripod The story of your fighting is a poem of two words: YOU SUCK.

            R Offline
            R Offline
            Roger Alsing 0
            wrote on last edited by
            #5

            This is not what I want to do. Im starting up some slow remote task async and I need the result from all of them before the method can return its result. So Im not asking how to show forms or anything, just whats the correct way to wait for the result of all of them while still inside the method that fired up the async tasks.

            S G 2 Replies Last reply
            0
            • G Gareth H

              Roger Alsing, You could use the AsyncCallback. http://msdn.microsoft.com/en-us/library/system.asynccallback.aspx[^] Regards, Gareth.

              R Offline
              R Offline
              Roger Alsing 0
              wrote on last edited by
              #6

              I know how to use async callbacks. But what I need is to block the execution just like in my sample: foo() { start async tasks wait for all threads //<-- that part return processed result }

              1 Reply Last reply
              0
              • R Roger Alsing 0

                This is not what I want to do. Im starting up some slow remote task async and I need the result from all of them before the method can return its result. So Im not asking how to show forms or anything, just whats the correct way to wait for the result of all of them while still inside the method that fired up the async tasks.

                S Offline
                S Offline
                Spacix One
                wrote on last edited by
                #7

                If you block execution of the calling method right after you make the thread. My question to you is: Why did you make it another thread?


                -Spacix All your skynet questions[^] belong to solved


                I dislike the black-and-white voting system on questions/answers. X|


                R 1 Reply Last reply
                0
                • R Roger Alsing 0

                  This is not what I want to do. Im starting up some slow remote task async and I need the result from all of them before the method can return its result. So Im not asking how to show forms or anything, just whats the correct way to wait for the result of all of them while still inside the method that fired up the async tasks.

                  G Offline
                  G Offline
                  GuyThiebaut
                  wrote on last edited by
                  #8

                  Greeeg's answer of Join is what I would suggest you look into. The Join method basically waits until other threads are finished before resuming executin on the current thread

                  Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
                  S 1 Reply Last reply
                  0
                  • G GuyThiebaut

                    Greeeg's answer of Join is what I would suggest you look into. The Join method basically waits until other threads are finished before resuming executin on the current thread

                    Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
                    S Offline
                    S Offline
                    Spacix One
                    wrote on last edited by
                    #9

                    If you go with a ISyncResult object with a delegate called through begineinvoke then you can use waitone and do the same thing... There are multiple ways to do what the OP asked, which method to use is mostly based off of the context of what you are doing.


                    -Spacix All your skynet questions[^] belong to solved


                    I dislike the black-and-white voting system on questions/answers. X|


                    1 Reply Last reply
                    0
                    • S Spacix One

                      If you block execution of the calling method right after you make the thread. My question to you is: Why did you make it another thread?


                      -Spacix All your skynet questions[^] belong to solved


                      I dislike the black-and-white voting system on questions/answers. X|


                      R Offline
                      R Offline
                      Roger Alsing 0
                      wrote on last edited by
                      #10

                      Im firing up multiple async tasks, so its not just one thread. its multiple remote services that are called async and I need to wait for all of them to finish so I can pick up the result and process it and then finally return the processed result from my method.

                      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