I have been working through the CS106B course from Stanford, and while completing the Boggle assignment, I have noticed that the Sleep() function on Windows behaves differently from the Pause() function. For testing purposes, I have simply set up the board and used the provided gboggle.h file to highlight the Boggle cubes, then remove the highlighting. The following is the relevant code:
for(int row = 0; row < board.numRows(); row++)
{
for(int col = 0; col < board.numCols(); col++)
{
HighlightCube(row, col, true);
}
}
Pause(0.5);
for(int row = 0; row < board.numRows(); row++)
{
for(int col = 0; col < board.numCols(); col++)
{
HighlightCube(row, col, false);
}
}
If I use Pause(), the cubes highlight, then return to normal. If I use Sleep() or Wait(), the cubes never highlight, and the delay in the program occurs before the board is even drawn rather than between the for loops. The relevant Wait() function:
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
taken from here. I am using Visual Studio 2005 on Windows XP.
What difference between these functions causes them to act this way?
Edit: I am aware that Sleep and wait require integers. I have tested them using integers and see a delay, but it occurs before the squares are written. Sorry I was not clear about that previously.
Edit2: After looking through some of the other libraries I used, I found that Pause is, in fact, part of the graphics library that simply pauses the graphics buffer.
I’ve never seen the Pause command before; perhaps you could provide some code for it?
Windows apps work on the idea of a message pump, and that painting is a low priority.
If you sleep or wait in the message pump thread then you block it from doing any further handling of messages such as drawing the screen.
You need to yield to the message pump so it can do it’s work.
You might look at usage of Wait for multiple and running a second message pump. (guessing this is the body of Pause).