-
Bout timers/interrupts
I want to make a program that will execute a function after 10 minutes. I mean along the process it will pause after ten minutes then execute a certain function. After execution it will resume the other process then count another 10 min. I tried using the delay(); and sleep(); function but it didnt worked. Is there any function that would be this possible?
ex
#include <stdio.h>
main()
{
clrscr();
/ * start counting for 10 minutes then execute a certain fxn*/
/* resume where it stopped */
}
:confused:
-
IIRC, sleep() is in milliseconds, so 10 minutes would be sleep(600000);
If the problem isn't this, please state the problem clearly instead of saying "it didnt worked."
-
You could also use the GetTickCount api.. this example pauses for 10 seconds and then quits:
Code:
#include <windows.h>
int main(){
DWORD start = GetTickCount();
for(;;)
if((GetTickCount() - start) > 10000)
break;
return 1;
}
Alternativly you could use the time functions or as said above, the sleep function...
-
That code will just go round and round and round at 100% CPU.
Under Windows, calling Sleep(0) will tell the scheduler you don't want any more of your time-slice, hence you can get away with:
Code:
#include <windows.h>
int main() {
DWORD start = GetTickCount();
for(;;) {
if((GetTickCount() - start) > 10000)
break;
Sleep(0);
}
return 0; // return 0 for normal exit, 1 is an error condition
}
-
Thanks for correcting me mike, didn't know about the sleep(0) thing.. ah and also, I always mix up return 1 and return 0 ;) this was the last time hehe (nullrmal :))
-
Thanks guys! Ill try this one... I'll keep in touch...:)
-
If you're already using Sleep, why not simply sleep for 8000 ticks and then enter a fast loop? This way you don't even start CPU shifts.