Reputation: 10054
I know this might be a bit of a 'silly' question, but sometimes, I just want to loop until a condition is false but I don't like keeping the loop empty. So instead of:
Visible = true;
while(IsRunning)
{
}
Visible = false;
I usually prefer:
while(IsRunning)
{
Visible = true;
}
Visible = false;
However, I'm a bit concerned about what exactly happens at the line Visible = true;
. Does the Runtime keep executing that statement even though it's redundant? Is it even advisable to do it this way? Many 'code-enhancements' plugins don't like empty while loops, and I don't really understand why.
SO:
Is there anything wrong with having an empty while loop?
Does having a loop such as shown above have any performance effects?
Thanks!
Upvotes: 5
Views: 15680
Reputation: 2880
Don't do it in main (UI thread). Try to find or make event (for ex. IsRunningChanged).
If you are need to wait something in thread it is better to use EventWaitHandle http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.eventwaithandle.aspx
But if you need to wait for bool value you can do
while(IsRunning)
{
Thread.Sleep(10);
}
Upvotes: 1
Reputation: 10512
Well..
There are alternate solutions, but without knowing your program it's hard to advise. You might however want to look into IPC and generally use blocking methods (call them in separate threads) instead of non-blocking.
Blocking methods pause your thread and give control back to the operating system which will (hopefully) do something useful with the CPU time, like putting parts of the CPU to sleep, saving power and reducing heat output.
Upvotes: 1
Reputation: 2436
It's not a good idea to do this. Start up the application, and when it enters this loop, take a look at the CPU usage in the task manager. You'll notice that one of the CPU cores will be running at full capacity. This means that energy is being wasted, and some other programs that may need to execute would not have as much CPU time as they could.
There are several ways around this. The simplest solution here is to put the thread to sleep for some number of milliseconds within each loop pass, like this:
Visible = true;
while(IsRunning)
{
Thread.Sleep(50 /* millisec */);
}
Visible = false;
A better solution would depend on what exactly your program is doing. If it's loading something in another thread, it may be better to either use AutoResetEvent
, or through locking, or other thread synchronization mechanism.
Upvotes: 1
Reputation: 12849
This is called busy or spin waiting.
The main problem is, that you are blocking the thread, that is running this while loop. If this thread is GUI thread, then it will result in freezing and unresponsible GUI. And yes, this results in loss of performance, because neither compiler, nor OS can know if they can somehow optimize this while loop.
Just try making single loop like this and see how one core of your CPU is running at 100%.
The right way to do this is to use asynchronous programming, where your code gets notified, either by event or language construct (like await) when the previous operation finished running.
But it is usually used in low-level systems or languages. .NET 4.0 introduces SpinWait construct, that serves as efficient threading sychnronization.
And in relation to your question. I believe neither should be used. They are both problematic for both debugging, performance and clarity of code.
Upvotes: 7