Question

I can't seem to tell why I can't stop the timer I have created using DeleteTimerQueue or any vesrion of it, such as DeleteTimerQueueTimer or Ex. Of course I fill in the arguments as needed.

At first I wanted to stop/delete the timer at a certain condition, but I have since removed that condition(an if loop) to see if Delete is working and it is not.

I am calling the Delete within the callback and I have two timers I want to stop at the same time the moment one of them accomplishes a task (that if Loop I'm talking about).

Anyway here is how I create the timers

    BOOL champ1_Success = ::CreateTimerQueueTimer(
        &baseAttackTimerHandler,
        NULL,
        callBaseAttackC1,
        this,
        0,
        (DWORD)(1000/attackSpeed),
        WT_EXECUTEDEFAULT);
    BOOL champ2_Success = ::CreateTimerQueueTimer(
        &baseAttackTimerHandler,
        NULL,
        callBaseAttackC2,
        this,
        0,
        (DWORD)(1000/attackSpeed),
                    WT_EXECUTEDEFAULT);

They work great! If only I could stop them.

Within their callbacks I call

    void CALLBACK Controller::callBaseAttackC1(void* lpParametar,
BOOLEAN TimerOrWaitFired)
    {
// This is used only to call QueueTimerHandler
// Typically, this function is static member of CTimersDlg
Controller* obj = (Controller*) lpParametar;
if (!obj->yi->isAlive() && !obj->skarner->isAlive()){
    if (!DeleteTimerQueueTimer(baseAttackTimerHandler, NULL, NULL)){
        printf("Base Attack Timer failed (%d)\n", GetLastError());
        obj->out << "Base A Timer Failed " << GetLastError() << endl;
    }
    //CloseHandle(baseAttackTimerHandler);
}
obj->basicAttackC1_TimeHandler();

}

 Note the 
       if (!DeleteTimerQueueTimer(baseAttackTimerHandler, NULL, NULL)){
          printf("Base Attack Timer failed (%d)\n", GetLastError());
          obj->out << "Base A Timer Failed " << GetLastError() << endl;
        }

I figure that calling the Delete within the callback would mean the timer only executes once, but it is not. It just keeps going. Another thing to note, and I'm not sure whether its good , bad or what it means but I'm have both different timers using the same Pointer to a handle, HANDL object I've created (HANDLE baseAttackTimerHandler = NULL;). I've tried making them of their own unique but it made no difference so I opted to have them use the same. BTW, DeleteTimerQueue Keeps failing when called. Error Code 87?

Bottom line is I really need them to stop, any idea what I am doing wrong or tricks to get them too?

EDIT: So I got it to work like this...

    if (!DeleteTimerQueueTimer(NULL, baseAttackTimerHandler_C1, NULL)){
        printf("2-Base Attack Timer failed (%d)\n", GetLastError());
        obj->out << "2-Base A Timer Failed " << GetLastError() << endl;
    }

but I had to make different handles for each timer to prevent error. (C1 and C2). Even though it puts out an error the first time, it stops after one execution.

However, putting it under my if loop prevents it from working. Going to have to look into my if loop. EDIT: Figured out what was wrong with my if loop. Since the callback seems to have to create an object of the class its in (Controller), its different than the one I initialized and am working with. So even though I do stuff with Controller con I make a Controller obj. Results in obj not the same as con and my if loop never results in true. Going to have to figure this one out. If there were a way to make the callback not be static and still use it in CreateTimer I could call con.

EDIT: Just used a strcut to make an object to set the obj equal to con indirectly. Works. I'm almost done, The timer stops but now my pop up message keeps getting spammed. I think I'm done with this thread though.

Was it helpful?

Solution

Yes, you have to use a different handle for each timer. Your original code has:

CreateTimerQueueTimer(&baseAttackTimerHandler ...);
CreateTimerQueueTimer(&baseAttackTimerHandler ...);

The second call was overwriting the handle from the first call. So when you called DeleteTimerQueueTimer to delete the timer, the handle you passed would delete the second timer but not the first.

Your solution is correct: use a different variable to hold each timer handle. That shouldn't be surprising. How can you expect to store two different handles in a single handle value?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top