Waiting for other threads
-
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 }
-
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 }
-
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 }
-
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 }
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 theBackgroundWorker
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 theRunWorkerCompleted
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 untilmyWorker_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.
-
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 theBackgroundWorker
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 theRunWorkerCompleted
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 untilmyWorker_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.
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.
-
Roger Alsing, You could use the AsyncCallback. http://msdn.microsoft.com/en-us/library/system.asynccallback.aspx[^] Regards, Gareth.
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 }
-
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.
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|
-
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.
Greeeg's answer of
Join
is what I would suggest you look into. TheJoin
method basically waits until other threads are finished before resuming executin on the current threadContinuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
-
Greeeg's answer of
Join
is what I would suggest you look into. TheJoin
method basically waits until other threads are finished before resuming executin on the current threadContinuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
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|
-
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|
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.