Solved

Timer Function

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 149
Detect CR LF to each line 12 175
Sed question 2 123
Not needed 13 129
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 …
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.

733 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