Execute a function every X seconds WITHOUT sleep

Hey guys,

My program occasionally has to do a little bit of heavy lifting processing wise, and I'd like a function that says something to the effect of

Now 40% complete...
Now 83% complete...

etc.

I have the function written and it works, but it calls so fast that the screen just fills up with

Now 1%
Now 2%
Now 3%

etc.

What I would like is the function to only be called once per second. HOWEVER what I DON'T want to do is use the sleep() or a wait() function because that will stop the programming from running altogether. I want something more along the lines of...

 
bool onSecond() // returns if clock is on a second 

1
2
3
4
while(loading){
   if(onSecond())
        displayPercentageLoaded()
}


Do you see what I mean? Every answer I find about timing always has to do with pausing or sleeping, but that's not what I'm looking for.


Look forward to hearing your thoughts.

Much thanks!
Last edited on
Last edited on
Sorry, but, you may try :
1
2
3
4
5
6
7
8
9
10
11
12
int ms = 100; //milliseconds remaining
while(...)
{
Sleep(10); 
//Update window, message etc...
ms = ms - 10;
if (ms <= 0)//Time out
{
percent = percent + 1;
Display();
ms = 100;
}

Also you may try the another method : Create a calculation thread and do the process bar code, instead of sleeping the main thread. That's better.

Edit : If you want to use the function "settimer", specify and handle the message WM_TIMER
Last edited on
Thanks for the ideas but I'm just going to scrap this feature. Doesn't seem like it's worth the trouble. Thanks anyway!
Your original idea was a good one, and its not hard to do. You just need to use a seperate thread of excution for the processing. You can look at the CreateThread() Api function. If you don't try it you won't learn anything. I don't have time to post code now, but perhaps late tomorrow.

If you don't try it you won't learn anything.


Very true, sir! And it does sound like a very good thing to know :)

This particular assignment is due in 2 hours, so I don't think I'm going to learn it before then, but you are definitely correct.
Topic archived. No new replies allowed.