Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Timer Function

Posted on 2000-03-28
4
Medium Priority
?
536 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 300 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

782 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