Hi,

I have a general question about for example a continuous while loop.

If we have a while(true) loop that will keep going for 24 hours without any
Thread.Sleep(), where the processor will be completely busy all the time.

Is this a good or bad practise, or is it okay to do that or should such work
have a Thread.Sleep(), every for example 5 minutes or so for any unknown reasons to me?

while(true)
{

    //will work for 24 hours and put constant pressure on the CPU

}

What are you trying to do? Are you trying to test your CPU? or making the program to test the CPU out general fun-ness?

This loop is processing and reading text files, alot of them. So it can take for example 24 hours.

As it is now, the pressure on the CPU is constant which leaves me wonder if that is correct to do, -or if this kind of work should be combined with random Sleep() for example?

This is a general question about a practice like this example.

The loop is inconsequential. If it takes 24 hours to process your data then it takes 24 hours to process your data.
If your loop is sat there doing nothing, then you need to re-think your design.

So something like;

while(moreDataToProcess)
{
    ProcessFile(); // Does not return until file is processed.
}

Is fine. But;

while(moreDataToProcess)
{
    // Empty loop just checking condition
}

Is wrong.

The loop I have is full with code and processes calculations.
So then I beleive it is okay without a pause in the loop now and then.

Thank you for the insight

So then I beleive it is okay without a pause in the loop

Not really -- without a Thread.Sleep() other processes don't have much of a chance to get CPU time. You can easily test this by putting Thread.Sleep(0) inside the loop then check Task Manager to see what affect it has on the CPU time your program consumes.

Anyway, have in mind the info of this article to monitor the system performance even is related to W2003, is still valid for all the versions of server and also client after NT 4.

Hope this helps

Not really -- without a Thread.Sleep() other processes don't have much of a chance to get CPU time. You can easily test this by putting Thread.Sleep(0) inside the loop then check Task Manager to see what affect it has on the CPU time your program consumes.

Depends how you want to marshal your task scheduling. Windows itself is pretty clever and can offer time slices to other processes but this depends on the intensity of his work.

For example, while I write this I have a console application running 4 threads in an infinite loop that do nothing other than set a boolean flag, check it and do nothing with the result, consuming 99% of my processor time.
There is no degradation of system performance as Windows can easily marshal processing time to other processes. When you start doing heavy IO work then Windows starts to get bogged down. But this is not the loop taking up your processing time, but the actual "meat" of the software.

The idea behind the OP's solution is that the time it takes to process a single iteration is far more intensive than the looping itself. If you want to sleep inside your intensive operations to marshal CPU time then that would be okay. But I wouldn't just sleep the loop, because once you reach this stage, Windows can easily marshal time away and place your software on a lower priority. It kinda depends on your requirements I guess. Sleep if you want to make other applications much more responsive (transparent processing), don't sleep if you want to finish processing as quickly as possible.

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.