Solved

Timer Function

Posted on 2000-03-28
4
499 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now