creating a pause in a loop, everytime it loops

matt2kjones
matt2kjones used Ask the Experts™
on
OK i have a loop, i have coded an eaxmple here to illistrate what imt trying to do:

 while(counter < 10)
 {
  counter++;
  txtStartTime->Text = counter;
  Sleep(500);
 }


ok basically what i want to happen is, everytime it loops, i want it to pause for 5 secs. Now, i thought sleep would do this, but it doesn't. It seems to be sleeping for 5 secs, then the loops loops around at normal speed.

instead of loop, sleep, loop, sleep, loop, sleep, loop, sleep

what should happen is txtStartTime (which is an object in a gui) should display 1 straight away, 5 secs later, should display 2, 5 secs later, should display 3, 5 secs later, should display 4

and so on

whats happening now is, i click the command button which executes the loop, but, there is just a 5 sec pause, then 10 is put into txtStartTime->Text

anyone know of a function i could use to achieve what i want

thanx :)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012
Commented:
>>i want it to pause for 5 secs

You'd use "Sleep(5000);" then...
Commented:
500 milliseconds is only a half second wait.  Try 5000.

Gary
bxt

Commented:
Your problem is that you are not yielding to the operating system to allow it to update the GUI.  So the only time it updates is after the loop is done.  What you should really do is have this loop running in a seperate thread posting update messages to the GUI thread; but this should work as a workaround.

Try this

while(counter < 10)
{
 counter++;
 txtStartTime->Text = counter;
    MSG msg;
    while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
        if (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE)
              return 1;
        if (msg.message == WM_SYSCOMMAND && (msg.wParam & 0xFFF0) == SC_CLOSE)
              return 1;

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

 Sleep(500);
}
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: jkr {http:#9252851} & GaryFx {http:#9252856}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
bxt

Commented:
Seems a bit wierd that you are not recommending any points for my answer since

1) I'm pretty sure it is correct
2) It has a lot more detail than the other two answers

Commented:
Yes bxt, I agree with you that it is both

1) correct
2) more detailed.

However, I think that an answer to the question has already been reached by the time your posted your comment.

I guess that with the 2 comments I recommended the question is answered and thats why I splitted between the 2 of them.

If question would have been worth of some more points I would have possibly recommended your answer also in the split, but as it is a 50 points question, there are not enough points to split between all of you.

Anyway, mine is just a recommendation. When moderator come to close it, then he'll decide. Just wanted to make my recommendation clear.

Tincho

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial