Sign in to follow this  

Why isn't this working?

This topic is 3661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
}

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this