Safe Win32 Timer

Environment: Win32, C++

Hello guys. This article presents some Win32 timer-related stuff. It consists of two classes:

  1. CTimerHost -- that class is the source of "safe" timers. That means you can kill timer and be sure that you will not receive any timer notification from already dead timer. That timer should be used and destroyed only from the thread it was created.
  2. CTimerThunk<...> -- represents thunk for receiving timer notifications.

These classes uses dynamic thunking technology. That means total absence of any static maps or lists. Enjoy.

Example of usage:

#include "sf_timer.h"

// for example in your CApp
CTimerHost g_TimerHost;

class CMyClass
    CMyClass() : m_Timer(g_TimerHost, this, OnTimer)

    void Stop()

    void OnTimer(DWORD dwTime) {}

    CTimerThunk<CMyClass>   m_Timer;


Download source - 5.17 Kb


  • No portability...

    Posted by Legacy on 08/01/2001 12:00am

    Originally posted by: Paul

    While this method is certainly a nice trick, it is not easily portable to other operating systems or other processor architectures.  When I am dealing with one of the Windows function that requires the use of a callback, I favor using a map from the standard C++ library.  For example, if you were setting up a WndProc callback function, you could make the map reference the hWnd parameter, and the stored information the address of the class instance:

    class Window

    Window( );
    virtual ~Window( );

    // Register the window class...
    void Register( )
    wc.lpfnWndProc = Window::WndProc;
    ::RegisterClassEx( &wc );

    // Create the window...
    HWND Create( )
    g_window = this;
    m_hWnd = ::CreateWindowEx( ... );
    ::ShowWindow( m_hWnd, SW_SHOW );
    ::UpdateWindow( m_hWnd );
    return( m_hWnd );

    // The class callback...
    LRESULT Switch( UINT uMsg, WPARAM wParam, LPARAM lParam )
    switch( uMsg )
    case WM_CREATE:

    return( 0 );

    case WM_DESTROY:

    return( 0 );


    return( DefWindowProc( m_hWnd, uMsg, wParam, lParam ) );


    HWND m_hWnd;
    std::map< Window* > m_map;

    static Window* m_window = 0;

    // The real callback
    static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
    if( ( uMsg == WM_CREATE ) && ( Window::m_window != 0 ) )
    m_map[hWnd] = Window::m_window;
    Window::m_window = 0;

    return( ( m_map[hWnd] )->Switch( uMsg, wParam, lParam ) );

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date