WM_TIMER message called but no timer set

I am writing a fairly simple windows C++ application in Visual Studio Express 2013, and at startup have a problem where the window hangs for 5-10 seconds due to a DispatchMessage processing a WM_TIMER message. The problem is, I never created a timer, so I'm not sure why I'm getting the message nor why there is this long hang.

enum KRESULT
{
    K_OK,
    K_FALSE,
    K_QUIT
};

HINSTANCE g_hInstance;
HWND g_hWnd;
char* g_winName;
char* g_name;

KRESULT MessagePump( MSG &msg )
{
    if( !PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ))
    {
        // there weren't any messages
        return K_FALSE;
    }
    else
    {
        if( msg.message == WM_QUIT )
            return K_QUIT;
        if( msg.message == WM_CLOSE )
            return K_QUIT;

        TranslateMessage( &msg );
        DispatchMessage( &msg );
    } 
    return K_OK;
}

void Run()
{
    MSG msg;

    bool done = false;
    while( !done )
    {
        KRESULT msgRes = K_OK;
        while( msgRes != K_FALSE )
        {
            msgRes = MessagePump( msg );
            if( msgRes == K_QUIT )
            {
                done = true;
                break;
            }

            done = !DoFrame();
        }
    }
}

bool DoFrame()
{
    // do a bunch of stuff, disabled for debugging.
    // returns false if Esc is pressed
    return true;
}

LRESULT CALLBACK MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch( uMsg )
    {
    // Code managing WM_SIZE, WM_ENTERSIZEMOVE, etc that has to do with changing the size of the window.
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    bool bRunDefault = false;

    switch( uMsg )
    {
        // check if the window is being destroyed.
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }

            // check if the window is being closed.
            case WM_CLOSE:
            {
                PostQuitMessage(0);     
                return 0;
            }

            default:
            {
                bRunDefault = true;
                break;
            }
        }
    }

    if( bRunDefault )
        return MessageHandler(hWnd, uMsg, wParam, lParam);

    return 0;
}

void InitWindows()
{
    WNDCLASSEX wc;
    DEVMODE dmScreenSettings;
    int posX, posY;

    // get the instance of this application
    g_hInstance = GetModuleHandle(NULL);

    // setup the windows class with some default settings
    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = g_hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_WINLOGO);
    wc.hIconSm       = wc.hIcon;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_name;
    wc.cbSize        = sizeof(WNDCLASSEX);

    // register the window class
    RegisterClassEx(&wc)

    // determine the resolution of the client desktop screen
    int screenWidth  = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    int wWidth = 1366;
    int wHeight = 768;

    DWORD styleFlags;

    // place it in the middle of the screen
    posX = ( screenWidth - wWidth ) / 2;
    posY = ( screenHeight - wHeight ) / 2;

    styleFlags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;

    // create the window with the screen settings and get the handle
    g_hWnd = CreateWindowEx( WS_EX_APPWINDOW, g_name, g_winName, styleFlags,
                             posX, posY, wWidth, wHeight, NULL, NULL,
                             g_hInstance, NULL );

    // bring the window up on the screen and set it as main focus
    ShowWindow( g_hWnd, SW_SHOW );
    SetForegroundWindow( g_hWnd );
    SetFocus( g_hWnd );

    // mouse cursor options
    ShowCursor( true );
}

Sorry about all the code, I wasn't sure how much of it was relevant. Most of these functions are encapsulated in an app class, and my WinMain function creates an instance of the class, calls InitWindows(), and Run(). The place where it hangs is in the MessagePump function, at DispatchMessage(). The WM_TIMER msg is passed twice, each with the following parameters:

hwnd = 0x00000000 <NULL>
message = 275 (WM_TIMER)
wParam = 18876 (this is random, but varies from around 18000 to 22000 or so)
lParam = 1975757148 (this is always the same)

Then, it is passed once with wParam = 1 and lParam = 0. Always in this order, and this group of three WM_TIMER messages are called every few frames.

The hanging only happens once, however, on the first time the message is passed at the beginning.

The hanging is the worst in debug mode of course, but in Release build, it happens as well.

I can add a handler to the MessagePump function, like so:

if( msg.message == WM_TIMER )
    return K_OK;

This works to stop the hanging, but then it creates another problem where, when I move the window, my whole OS freezes for about 5-10 seconds.

I have been programming for a while (by no means an expert) but this baffles me. Any help is appreciated. Thanks!

Edit: I tried building and running on two other machines. No hanging on both. After several restarts of the original machine, removing the startup processes that I felt I didn't need, the problem finally stopped, and has remained OK even after ~50 runs. I wish it was more satisfying to report which process was causing it, but I still haven't figured it out.

There was no process at the address 75C3A95C when looking at the modules window, strangely.

Something I noticed though: on all machines, the WM_TIMER messages are being passed. In addition, even with the default project you can create in VS2013 Exp, Win32Project1, these 3 WM_TIMER messages are being passed. Also, the Win32Project1 did hang as well, so it really had nothing to do with my code, but I guess... some strange interaction with a process running in my machine?

Thanks for everyone's help.

Answers


Perhaps another running program is sending yours the WM_TIMER messages, or perhaps a library you are linking in. Try running on on another, clean machine.


Need Your Help

C library vs WinApi

c++ c winapi visual-c++

Many of the standard c library (fwrite, memset, malloc) functions have direct equivalents in the windows API (WriteFile, FillMemory/ ZeroMemory, GlobalAlloc).

ASP MVC in IIS 7 results in: HTTP Error 403.14 - Forbidden

asp.net-mvc iis hosting

I'm developing an ASP MVC web project. Now I have a requirement which forces me to deploy to an IIS7 inmiddle of development (to check some features). I'm getting the above mentioned error message