Use of multiple timers

Source: Internet
Author: User
Tags time interval

This article supporting source code

First, the basic use of the Timer method

When you are programming, you will often use the timer. The method of using timers is relatively simple, usually tells Windows a time interval, and then Windows periodically triggers the program at this time interval. There are usually two ways to do this: send Wm_timer messages and invoke application-defined callback functions.

1.1 Use Wm_timer to set timer

First, look at the prototype of this API function SetTimer.

UINT_PTR SetTimer(
 HWND hWnd,       // 窗口句柄
 UINT_PTR nIDEvent,   // 定时器ID,多个定时器时,可以通过该ID判断是哪个定时器
 UINT uElapse,      // 时间间隔,单位为毫秒
 TIMERPROC lpTimerFunc  // 回调函数
);

For example

SetTimer(m_hWnd,1,1000,NULL); //一个1秒触发一次的定时器

In an MFC program, SetTimer is encapsulated in the CWnd class, and the call does not specify a window handle, for example:

UINT SetTimer(1,100,NULL);

The function return value is the first parameter value of 1, which represents the ID number of this timer.

The second parameter indicates that you want to wait 100 milliseconds for the time to be processed again. The third parameter is generally null in this method.

Note: When you set the second parameter, be aware that if you set a wait time that is shorter than the processing time, the program will go wrong.

1.2 Calling the callback function

This method first writes a callback function in the following format

void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime);

Then use the SetTimer (1,100,TIMERPROC) function to build a timer, and the third parameter is the callback function address.

Realization and application of multiple timers

We have the ID assigned to the timer when we install it, and the ID works when we use multiple timers.

When you do not use MFC, when you receive a WM_TIMER message, the value in WPARAM WPARAM is the ID of the timer

Using MFC is simpler, we add wm_time message processing function OnTimer, see the following example

void CTimerTestDlg::OnTimer(UINT nIDEvent)
{
  switch (nIDEvent)
  {
  case 24: ///处理ID为24的定时器
    Draw1();
    break;
  case 25: ///处理ID为25的定时器
    Draw2();
    break;
  }
  CDialog::OnTimer(nIDEvent);
}

When you use the callback function, we can judge which timer is based on the value of Ntimerid, for example:

void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime)
{
  switch(nTimerid)
  {
  case 1: ///处理ID为1的定时器
     Do1();
     break;
  case 2: ///处理ID为2的定时器
     Do2();
     break;
  }
}

Third, cancel the timer

After we stop using the timer, we should call KillTimer to cancel the timer, the KillTimer prototype is as follows

BOOL KillTimer(
 HWND hWnd,     // 窗口句柄
 UINT_PTR uIDEvent  // ID
);

In the MFC program we can call KillTimer (int nidevent) directly to cancel the timer.

The example code provided in this article can be seen at run time with two timers working, and the first one to be killed 10 times after it is run.

Please see the example program for detailed code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.