Implement the timer of the Win32 console Program

Source: Internet
Author: User
Using a timer in the console cannot be a simple settimer. This settimer method in the console is a little troublesome and requires you to write messages cyclically to deliver wm_timer messages. In fact, you can use the multimedia clock in the console for timing:

Example:

// Start the timer
Mmresult nidtimerevent = timesetevent (
1000, // 1 second delay
0,
Timeproc,
0,
(Uint) time_periodic );
If (nidtimerevent = 0)
Cout <"Startup timer failed" <Endl;

// Callback process (when the clock arrives, the callback function is automatically called by the System)
Void callback timeproc (
Uint uid,
Uint umsg,
DWORD dwuser,
DWORD dw1,
DWORD dw2
)
{
Cout <"clock arrival" <Endl;

}

Of course, if you are used to settimer, use settimer:
The following is an example of using settimer in the console:
# Include <windows. h>
# Include <iostream>
Using namespace STD;
Void callback timeproc (
Hwnd,
Uint message,
Uint idtimer,
DWORD dwtime );
Int main ()
{
Settimer (null, 1,1000, timeproc );
MSG;
While (getmessage (& MSG, null, 0, 0 ))
{
If (msg. Message = wm_timer)
{
Dispatchmessage (& MSG );
}
}
Return 0;
}
Void callback timeproc (
Hwnd,
Uint message,
Uint idtimer,
DWORD dwtime)
{
Cout <"a timer comming" <Endl;
}

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.