Click here to Skip to main content
15,921,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In c++ program, i like to call a function of the same program but that has to be execute in periodic frequency(specified time interval)...

regards,
sanath
Posted
Updated 16-Sep-11 2:30am
v2

You may use a timer for this. See Using Timers at MSDN[^].
 
Share this answer
 
You have to do it in this way:
C++
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>

#pragma comment (lib,"user32.lib")
enum
{
  INTERVAL  = 2000// milliseconds
  MAX_COUNT = 10,
};


void FAR PASCAL __fnTimer(HWND, UINT, UINT_PTR, DWORD)
{
  static int  count = MAX_COUNT;
  if(0>=--count) PostQuitMessage(0);
  Beep(3000,100);
}

int FAR PASCAL _tWinMain(HINSTANCE h,HINSTANCE p,LPTSTR c,int sw)
{
  if(0<SetTimer(0,0,INTERVAL,__fnTimer))
  {
    for(MSG m;GetMessage(&m,0,0,0);DispatchMessage(&m));
  }
  return 0;
}

i assumed youre using windows.
Good luck.
 
Share this answer
 
by using timers, how can I call my function for every specified time?
 
Share this answer
 
Comments
Richard MacCutchan 17-Sep-11 9:22am    
Call it every time you receive a WM_TIMER message, or your timer function gets called.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900