Why isn't this working?

Started by
6 comments, last by iMalc 16 years, 5 months ago
I'm trying to create my own timer system, but when I run the program it totally like disapeers and stops when it calles TimerStartEx recursively... I'm trying to find a way to avoid the stupid while loop cause it causes the program to wait only for it. Unless someone can help me find a better solution please help :)
void TimerStartEx(timer T, float Duration, bool Periodic, void (*Function)(), DWORD InitialTimer)
{
        DWORD ExpireTime = (DWORD)(Duration * 1000.00);
        if (GetTickCount() < (InitialTimer + ExpireTime)) {
                         TimerStartEx(T, Duration, Periodic, (*Function), InitialTimer);
        } else {
               Function();
        }
}

void TimerStart(timer T, float Duration, bool Periodic, void (*Function)())
{
        DWORD InitialTimer = GetTickCount();
        DWORD ExpireTime = (DWORD)(Duration * 1000.00);
        
        TimerStartEx(T, Duration, Periodic, Function, InitialTimer);
}
-INFERNO
Advertisement
Stack overflow.

Your system will provide a function called Sleep(), sleep(), usleep() or something like that which you can call inside a loop to have the machine wait for a small amount of time.
The recursion has the same effect as iteration would: the function is called again, immediately. It doesn't matter how little time it takes to do something; if you do it constantly, then it will take up all your time.

This is why the OS provides functionality to say "suspend this process for at least this much time, and then pick up where it left off" - Sleep() (on Windows), as well as the concept of threads (far too much to explain in one post).
Two people have suggested the "sleep" function, which will solve the stack overflow...but it won't solve your problem.

First of all, this is *NOT* an example of where you should use recursion.

You are essentially just doing the following, which is a lot more readable, and doesn't use the stack so much:

void DelayFunction(clock_t delay, void (*Func)() ){  clock_t start = clock();  while (clock() - start < delay) ;  Func();}


Even if you add a sleep, the above code doesn't do what you want...because it is blocking and the calling process will have to wait. What you really should do is more like this:


Note..this is Pseudocode...you will need to use a thread library and the syntax for that varies
void DelayCall(struct Params){  clock_t delay = Params[0];  Func = Params[1];  clock_t start = clock();  while(clock() - start < delay)  Sleep(1);  Func();}void DelayFunction(clock_t delay, void (*Func)() ){   Params[0] = delay;   Params[1] = Func;   CreateThread(DelayCall, params);}
hmm can I use threads to do stuff in the background while not interrupting the flow of the main program? What I'm trying to do is like for that timer, when I create it, itll constantly run until I destroy it displaying the text or doing whatever. I'm trying to make it multitask all of the timers while still running through the main program flow.
-INFERNO
Ahh problem solved, I didn't know threads were "multitasking" and didn't try it until yahatsu mentioned it! Thanks a bunch :)
-INFERNO
Threads are complex, you have to be extremely careful with shared data or else you can end up with difficult to diagnose bugs.

What do you want to do in the background?
You might find that with limited knowledge of threading that you're safer to simply save the value of GetTickCount at the start, and then subtract the current value of GetTickCount in your main loop or wherever, doing whatever it is that you want to do when the difference is greater than a certain number of milliseconds.

Otherwise a good idea is to create a single TimeManager class with a thread with which you register timeouts and callbacks, and the TimeManager will simply wait until the soonest expiring callback is due, begin waiting for the next registered timeout, and do the callback.
You probably don't need to go to the trouble of creating a seperate thread for each of your timers anyway.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement