I’m creating three threads in one of my application. The requirement I have is that the method that creates these three threads and starts them off should not return unless all these three threads are executed.
I tried to use Join on all three threads. However, I observe that when I use Join the total execution time of my method is the sum of execution times of all three threads. In other words they are getting executed in sequence.
I tried using ThreadState but realized from MSDN and stackoverflow that ThreadState property should only be used for debugging purposes and not for real coding.
What is the best way I can achieve this and keep the execution parallel.
Any ideas will be much appreciated. Thanks in advance
That suggests you’re not starting the threads properly, or they’re performing locking which is preventing them from executing in parallel. Calling
Joinon each thread in turn is a simple and effective way of waiting for each thread to finish.One bug I can imagine is if you’re starting each thread and calling
Joinas soon as you’ve started it rather than waiting until you’ve started all the threads. For example:Instead, you should start all three threads and then join all of them:
Does that explain your issue? If not, please post code showing what you’re doing.