Scenario:
I hav set a timer using SetTimer(),after timeout my application gets a WM_TIMER and it exits.
I Want: to pause this timer before a particular process and again restart it when the process gets over,so tht there is no timeout during this process and my application doesnt exit during this process.how can i pause a timer and again restart it starting from the paused time.Plzzzzz help........

Dani AI

Generated

A reliable way to “pause” a Win32 timer and resume from the remaining time is to record when the timer was started, compute elapsed time when pausing, kill the timer, and then restart it with the remaining interval. This preserves the original deadline instead of restarting from zero. ’s flag trick is useful to ignore handler work while a task runs, but it does not freeze the countdown; the timestamp method below does.

// keep as members/globals
UINT_PTR tid = 1;
DWORD origMs = 60000;
DWORD remainingMs = origMs;
unsigned long long startTick = 0;
bool paused = false;

// start
startTick = GetTickCount64();
SetTimer(hwnd, tid, origMs, NULL);

// pause
if (!paused) {
    paused = true;
    unsigned long long now = GetTickCount64();
    DWORD elapsed = (DWORD)(now - startTick);
    remainingMs = (elapsed >= origMs) ? 0u : (origMs - elapsed);
    // mark state and remove any already-queued WM_TIMER messages for this id
    KillTimer(hwnd, tid);
    MSG m;
    while (PeekMessage(&m, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE)) { /* drop queued timers */ }
}

// resume
if (paused) {
    paused = false;
    startTick = GetTickCount64();
    SetTimer(hwnd, tid, remainingMs ? remainingMs : 1, NULL);
}

Notes and pitfalls: WM_TIMER messages can already be queued when you pause, so either set a paused flag that the handler checks or explicitly remove queued WM_TIMER messages (shown above). Use GetTickCount64 when available (or unsigned arithmetic with GetTickCount to handle wraparound). If you need greater precision or thread-based timers consider multimedia timers or waitable timers (timeSetEvent / CreateWaitableTimer) and reapply the same elapsed/remaining logic. Finally, when resuming if you want periodic behavior, re-arm the timer back to the original interval after the resumed one-shot interval fires.

Recommended Answers

All 3 Replies

One way is to stop the timer before the process starts then restart it after it finished

KillTimer(hWnd, uIDEvent); // stop the timer
// Do some processing here
SetTimer( hWnd, uIDEvent, uElapse, lpTimerFunc); // restart timer

Thanks for the solution but the SetTimer() will again start the timer from 0 and not from the time where it was stopped.

Another solution is to set a global (or class) flag that indicates whether the timer function should do anything. If the flag is TRUE then the timer function should just return without doing anything. otherwise if FALSE then do whatever it would normally do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.