redspah
redspah

Reputation: 277

How to create a Non-Freezing Infinite Loop?

My question is as follows:

How to create an infinite loop in C++ which doesn't freeze the window, keeps it responsive and allows for program to be closed at any time?

Example:

#include <Windows.h>
int main()
{
    while(1)
    {
        FlashPrettyGraphics();
        Sleep(10);
    }
}

This program, as it is right now, will work for approx. a dozen seconds, after which it will become unresponsive and crash. During the execution the window will be unable to be moved.

How do I solve this?

Upvotes: 2

Views: 286

Answers (1)

NathanOliver
NathanOliver

Reputation: 180415

This is generally accomplished with multi-threading. You create one thread that interacts with the user and then you create another thread that has the infinite loop that does the processing.

Upvotes: 4

Related Questions