I'm trying to seem if(t3.alive) is not ever entered. It successfully goes through t1 and t2 if statment but skips over three. I've tried the do while, while (like seen). I would think if t3 is still alive the loop would still go then once it's not, its out but i'm not seeing that result. even if it wasn't started it would at least go in immediately but it's just skiped over help please

           while ((t3.IsAlive) || ((t2.IsAlive) || (t1.IsAlive)))
        {
            if (!t3.IsAlive && itc == false)
            {
                it.Stop();
                itc = true;
            }
            if (!t1.IsAlive && mtc == false)
            {
                mt.Stop();
                mtc = true;
            }
            if (!t2.IsAlive && qtc == false)
            {
                qt.Stop();
                qtc = true;
            }



        };        

Recommended Answers

All 12 Replies

Maybe you need && instead of ||

if I use && the thread that is not still alive will have their clocks still running.

If you use logical and "&&" this means if you want to go into while loop all of three threads muste be alive.
And this something you dont want, right?
Maybe is that "itc" variable true? Is it possible?

right i dont want that, I want it to loop as long as atleast one is alive

Try without other boolan flags:

        if (!t3.IsAlive)
        {
            it.Stop();
            itc = true;
        }
        if (!t1.IsAlive)
        {
            mt.Stop();
            mtc = true;
        }
        if (!t2.IsAlive)
        {
            qt.Stop();
            qtc = true;
        }

You will see what will happen. If any of thoese 3 threads is allive, it should go into if statements.
If this is ok, that means that is something "wrong" with the other boolean flags (which are now removed).

Same problem. Can someone tell me if the while statement I have above is valid technically...

Depends on what you would like to check. If you want to check if at least on of the threads is alive, then this is the correct way.
The code will exit while loop only in case if ALL 3 threads will not be alive any longer.

Eeek. I sugguest putting a Thread.Sleep() in that loop to avoid murdering your CPU.

As for the actual problem, what logic are you trying to accomplish? You seem to have a bunch of extra brackets that are reducing clarity, even though logically none of them are required:

while (t3.IsAlive || t2.IsAlive || t1.IsAlive)
//do stuff

Is logically equivalent to your

while ((t3.IsAlive) || ((t2.IsAlive) || t1.IsAlive))
//do stuff

If you want the loop to die when t3 ends or when both t2 and t1 end then use:

while (t3.IsAlive && (t2.IsAlive || t1.IsAlive))
//do stuff

thanks

skatamatic I did have the first version you posted, however, I wanted to see if I would have different results.

It's not that I want the loop to die when the when t3 ends I would the loop to end when all three ends. Where do you suggest that I put thread.sleep? also If I use thread.sleep how does that effect the other threads as well as me timing the threads. the .stop() methods are part of the stopwatch instance.

skatamatic:
1st and 2nd examplea are equl (since you used logical OR operator, and braces does not make any difference.
While 3rd example is different: it says: loop while t3 and any of t1 or t2 are alive - use this example if you wanna check if your t3 is alive or not.
Or if you wanna check if all are alive

while (t3.IsAlive && t2.IsAlive && t1.IsAlive)

Well that means, then, that if one thread dies then the loop ends, that not what I want, I want it to end when all three end

I didn't say the third example was equal to the first 2...

You are correct to use logical OR ( || ) if you want to wait for all 3 to end. Your problem must be coming from somewhere besides the loop conditions. Maybe posting more relevant code would help (what are these threads/the rest of this method doing), and possibly doing some debugging on your own to see what is actually happening. I suspect t3 is terminating when you aren't expecting it to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.