Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Timer Function

Posted on 2000-03-28
4
Medium Priority
?
529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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: 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…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

721 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