Link to home
Start Free TrialLog in
Avatar of hp746
hp746

asked on

settimer in vc++

I have written code in vc++ using timer
my aim is it has to print the numbers until the time expires ex 10 seconds
but it is printitng only 0 and then after ten seconds i have seen the timer proc function called

please let me  know what is wrong?

i have to print values  until time expires
// timer.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
 
bool bStillBusy = false;
 
void CALLBACK TimerProc(HWND, UINT, UINT, DWORD);
void mynum();
int main()
{
 UINT id;
 MSG msg;
 
bStillBusy  = true;
 
 id = SetTimer(NULL, 0, 10000, (TIMERPROC) TimerProc);
 
 while(bStillBusy) 
 {
	 
	mynum();
 
	GetMessage(&msg, NULL, 0, 0);
	DispatchMessage(&msg);
	
 }
 
 KillTimer(NULL, id);
 
 return 0;
}
 
void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime) 
{
printf("Timer func got called\n");
 
bStillBusy = false;
}
 
 
void mynum()
{
 
int k = 0;
if( k < 50)
	 {
		printf("%i\n",k);
 
	 }
 
	 k =k+1;
 
 
 
}

Open in new window

Avatar of Anwar Saiah
Anwar Saiah

try making the printing take more time

void mynum()
{
 
int k = 0;
if( k < 5000)
         {
               if(k mod 100 == 0)
                printf("%i\n",k/100);
 
         }
 
         k =k+1;
 
 
 
}
This is a console application , so it doesn't have a message loop , don't use
GetMessage(&msg, NULL, 0, 0);
DispatchMessage(&msg);

Use Sleep(1) instead...
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial