Hi there, I'm having trouble figuring out the setTimer function. Does anyone know for an example of using it in a non-MFC application? I mean something simple like printing 'Hello World!' every 5 seconds or something just so I can get a better idea of how its all supposed to work. I've been staring at MSDN for a while now, done some googling but I can't seem to get off the ground, I don't even have sample code to share that's how lost I am at the moment.

Thanks for the help.

Here's an example win32 api project. The code not posted was just generated by vc++ 2010 express IDE and is not relevent. Note that the TimerProc() doesn't have to call KillTimer() but it should if the TimerProc() will take longer to process than the amount of time between calls to prevent re-entrant problems. You can always call SetTimer() again at the end of TimerProc() if needed.

VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
{
   KillTimer(hwnd,idEvent);
   MessageBox(hwnd,"Hello","Testing",MB_OK);

}

...
...
//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   if( SetTimer(hWnd,1,1000,TimerProc) == 0)
   {
       DWORD dwError = GetLastError();
       char buf[255] = {0};
       FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,dwError,0,buf,sizeof(buf),0);
       MessageBox(hWnd,buf,"Error",MB_OK);

   }

   return TRUE;
}
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.