views:

129

answers:

5

I have a situation in which I want to start 3 threads called: tr1, tr2 and tr3

I want tr2 to start after tr1 and tr3 to start after tr2.

How can I achieve this?

A: 

The simplest way is just to have a small sleep between the startups, that is how I "solved" the problem.

Another option is to start tr2 from tr1, and tr3 from tr2, after you've done the non thread-safe things.

How are they dependent on each other? Why don't you just have one thread?

Marcus Johansson
It's not recommended to use Thread.Sleep: http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx
Giorgi
A: 

What Possible reason you have to that? if you don't need the, to run in parallel why do you need 3 threads?

Any way - you can call thread1.Join() from thread2 and thread2.Join() from thread3 so each thread will wait for the previous one.

Itay
+1  A: 

Make each thread start the next one.

However, if they all run in sequence anyways, what is the reason you want to use multiple threads in the first place?

John
A: 

You can use WaitHandles to signal that a thread has completed working.

Giorgi
+1  A: 

In Fx4 you can use Tasks and the ContinueWith feature.

And while it does make sense to have Tasks (Jobs) that must be run in sequence, it does not seem so sensible for Threads. Why don't you use 1 Thread that executes m1(), m2() and m3() in sequence? Especially if you are on Fx <= 3.5

Another aspect here is error handling. The tasks library will handle that more or less invisible, but without it you need to take care.

Henk Holterman