开发者

How to Use Timer in MFC Dialog based Application?

开发者 https://www.devze.com 2023-03-30 15:04 出处:网络
I am developing MFC Dialog 开发者_C百科based application in Visual Studio 2008. I want to use timer that start on start of the application and continue to run and calls a function that performs my tas

I am developing MFC Dialog 开发者_C百科based application in Visual Studio 2008. I want to use timer that start on start of the application and continue to run and calls a function that performs my task? How can I do this?

Thanks


Just use SetTimer method, and pass two arguments: Timer ID (any number), and the timeout in milliseconds. Then write up OnTimer implementation, and an entry ON_WM_TIMER inside BEGIN_MESSAGE_MAP...END_MESSAGE_MAP.

CWnd::SetTimer takes 3 parameters, but only 2 are required. Pass third argument as NULL.

CWnd::OnTimer


_AFXWIN_INLINE UINT_PTR CWnd::SetTimer(UINT_PTR nIDEvent, UINT nElapse,
    void (CALLBACK* lpfnTimer)(HWND, UINT, UINT_PTR, DWORD))

You may want to do something like

UINT_PTR myTimer = SetTimer (1, 1000, null); // one event every 1000 ms = 1 s

and react to the ON_TIMER event in your window's event handler:

void CMyView::OnTimer (UINT_PTR nIdEvent)
{
if (nIdEvent == 1)
    // handle timer event
}

Alternatively you can pass a pointer to a function handling the timer events. Keeping the handle to the timer allows you to turn it off using KillTimer() in case you have to.


If you want to get the basic idea of using Timers, kindly have a look at this link and go through the step by step procedure on working with timers. After this, you should be able to use timers easily in your application whenever you want.

Link: http://www.functionx.com/visualc/controls/timer.htm

Hope this helps.

Cheers.

0

精彩评论

暂无评论...
验证码 换一张
取 消