causing the computer to freeze
-
While I`m running/debugging a c# app I`m developing in VS that has a while loop with no exit, a program freeze takes place when the while statement is reached, at the same time the OS continues to be responsive, is that because the computer is running an 64 bit OS (with threads on multicore). Would a while statement like that cause a 32 bit OS to freeze? Also if I`m running a recursive function with no exit from recursion the program will break with an `stack overflow` error. Why is the debugger interrupting the program in the case of the recursive function and doesn`t in the case of a while loop?
-
While I`m running/debugging a c# app I`m developing in VS that has a while loop with no exit, a program freeze takes place when the while statement is reached, at the same time the OS continues to be responsive, is that because the computer is running an 64 bit OS (with threads on multicore). Would a while statement like that cause a 32 bit OS to freeze? Also if I`m running a recursive function with no exit from recursion the program will break with an `stack overflow` error. Why is the debugger interrupting the program in the case of the recursive function and doesn`t in the case of a while loop?
With a "stack overflow", even the debugger can't continue.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
While I`m running/debugging a c# app I`m developing in VS that has a while loop with no exit, a program freeze takes place when the while statement is reached, at the same time the OS continues to be responsive, is that because the computer is running an 64 bit OS (with threads on multicore). Would a while statement like that cause a 32 bit OS to freeze? Also if I`m running a recursive function with no exit from recursion the program will break with an `stack overflow` error. Why is the debugger interrupting the program in the case of the recursive function and doesn`t in the case of a while loop?
A while loop isn't an error as far as the system is concerned - it just means that your display can't be updated and user input is ignored until the loop exist and the event handler is finished. The system doesn't "know" how long you will be looping for and has to assume that you intended that to happen! Unbounded recursion is different: each recursive call uses up an amount of some special memory called the stack - and when that runs out (and it does, pretty quickly) you app has to stop running because it can't execute any more code; it needs the stack to do anything. So it throws an exception - Stack overflow - which the debugger picks up for you and stops you app so you can see what is happening. You shouldn't use big loops in event handlers - they should be moved to a second thread so that the UI thread can continue to update the display and deal with user input. Have a look here: BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
While I`m running/debugging a c# app I`m developing in VS that has a while loop with no exit, a program freeze takes place when the while statement is reached, at the same time the OS continues to be responsive, is that because the computer is running an 64 bit OS (with threads on multicore). Would a while statement like that cause a 32 bit OS to freeze? Also if I`m running a recursive function with no exit from recursion the program will break with an `stack overflow` error. Why is the debugger interrupting the program in the case of the recursive function and doesn`t in the case of a while loop?
A while loop without some sort of exit will cause the application to freeze, but will leave the OS alone (as long as the stack is filled up, or a some sort of min/max value isn't reached). This will execute forever, freezing the app, but leaving the OS responsive:
public void MethodX(string x)
{
while (true)
{
}
}This will eventually crash the app with a stack overflow exception:
...
MethodX("");
...public void MethodX(string x)
{
while (true)
{
MethodX("1234567890");
}
}This will run forever:
public void MethodY()
{
int x = 0;
while (true)
{
x = x + 100000;
}
}This will eventually crash the app with an OutOfMemoryException:
...
MethodZ("1234567890");
...private static void MethodZ(string x)
{
while(true)
{
x = x + x;
Console.WriteLine(x.Length.ToString());
}
}".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
A while loop isn't an error as far as the system is concerned - it just means that your display can't be updated and user input is ignored until the loop exist and the event handler is finished. The system doesn't "know" how long you will be looping for and has to assume that you intended that to happen! Unbounded recursion is different: each recursive call uses up an amount of some special memory called the stack - and when that runs out (and it does, pretty quickly) you app has to stop running because it can't execute any more code; it needs the stack to do anything. So it throws an exception - Stack overflow - which the debugger picks up for you and stops you app so you can see what is happening. You shouldn't use big loops in event handlers - they should be moved to a second thread so that the UI thread can continue to update the display and deal with user input. Have a look here: BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
Thanks Griff that`s a good theoretical explanation
-
Thanks Griff that`s a good theoretical explanation
You're welcome!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
With a "stack overflow", even the debugger can't continue.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
with the right pair of boots and equipment for hostile environment you never know
-
While I`m running/debugging a c# app I`m developing in VS that has a while loop with no exit, a program freeze takes place when the while statement is reached, at the same time the OS continues to be responsive, is that because the computer is running an 64 bit OS (with threads on multicore). Would a while statement like that cause a 32 bit OS to freeze? Also if I`m running a recursive function with no exit from recursion the program will break with an `stack overflow` error. Why is the debugger interrupting the program in the case of the recursive function and doesn`t in the case of a while loop?