Solved

Timer Function

Posted on 2000-03-28
4
505 Views
Last Modified: 2013-11-20
Hi,
I want to use SetTimer Function in a win32 console application.  I got the code compiled and build without error. I used like this.. But it is not calling the TimerFunct function. Can you please send me sample code to user SetTimer function in win32 console application.. Thank you.. //SPrema.

CODE:

SetTimer(NULL,1, 10000,TimerFunct);      

callback Function:

__stdcall TimerFunct()
{
      printf("Timerfunc");
                return 0;
}

0
Comment
Question by:prema75
  • 2
4 Comments
 
LVL 1

Expert Comment

by:sunj
ID: 2666083
I guess SetTimer doesn't work with console application at all...
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2666101
> I guess SetTimer doesn't work with console application at all...

That's not correct. SetTimer() works fine in console applications.  You need to have a message pump, though.  Prema, does your console app have a message pump in the thread where you're expecting the timer to work?

..B ekiM
0
 

Expert Comment

by:mmg
ID: 2667922
perhaps you need to create a thread for it!
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 100 total points
ID: 2670317
This code will do what you've asked for.

--- begin file timer.cpp ---


// compile with
//    cl /W4 timer.cpp

#include <stdio.h>
#include <conio.h>

#define STRICT 1
#include <windows.h>

#pragma comment(lib, "USER32.LIB")
#pragma comment(lib, "KERNEL32.LIB")

void __stdcall MyTimerProc(HWND, UINT, UINT, DWORD dwTime)
{
   printf("Timer fired! at %lu\n", dwTime);
}

void main()
{
   printf("Strike any key to quit...\n");

   if (!SetTimer(NULL, 1, 1000, &MyTimerProc))
   {
      printf("Failed to create a timer!\n");
      return;
   }

   MSG msg;
   BOOL bWorking = TRUE;

   while (bWorking)
   {
      if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
      {
         if (msg.message == WM_QUIT)
            bWorking = FALSE;
         else
         {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
         }
      }

      if (_kbhit())
         bWorking = FALSE;
   }

   return;
}

--- end file timer.cpp ---

..B ekiM
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question