Infinite loops in windows XP

Started by
4 comments, last by bioagentX 21 years ago
I have windows XP, and for some reason, whenever I execute a program with an infinite loop, my computer crashes. Well, it''s not that it crashes, it''s that I can''t end the program. I can''t press the little x in the top right corner of the window, and I can''t even end the task from the tesk menu. Does anyone have a solution to my problem?
There are three types of people in this world, those who can count, and those who can't
Advertisement
Windows works with messages. When you click the X at the top right of the window, you are sending the application that created the window a message, saying you want the window to close. The messages get stored in a queue, ready for the application to collect them.

If your application is in an infinite loop, it has to check the message queue every so often if it''s going to take any action based on the messages.

Harry.
Harry.
don''t use an infinite loop

the infinite loop "blocks" the windows message processing so the window won''t respond.

Do it like this:


  bool imrunning = true;while (imrunning){    while (peekmessage(blah))    {        if (blablabla) imrunning = false;        TranslateMessage(foo);        Dispatchmessage(foo);    }    // process here}// this is pseudo code of course  


If, with infinite loop, you are not referring to your game loop you might want to use threads... (google / mdsn)

In windows programming, actions are based on messages. If your program goes into an infinite loop, it wont receive/generate messages(such as "close window" message that is generated by clicking the upper right cross on the window ). And therefore cannot process it!

If you need an infinite loop, you probably need multithreading.

Oztan.
build in a mechanism by which your program can end the loop and exit gracefully. writing a program that can only be ended by killing the window is bad form b/c it means you''ll soon end up with memory leaks.

just do something like:


  bool endLoop = false;void killProgram() {    endLoop = true;}void doMemoryCleanup() {    //do any memory cleanup on global vars here}int mainLoop() {    while(1) {        if (endLoop)            break;        //do loop stuff    }    doMemoryCleanup();}  


in your WinProc method make the pressing of the ESC key call the killProgram() method. or instead of checking if (endLoop) do something like if (isKeyDown(''VK_ESC'')) break;

for info on WinProc, key detection stuff and a different way to exit infinite loops, check out the nehe tutorials on this site which implement said items

-me
Hey those 2 messages were not there! Took 4mins to post a message!

This topic is closed to new replies.

Advertisement