WIN10 Series: VC + + Timer

Source: Internet
Author: User

The timer mechanism, commonly known as "heartbeat," is a mechanism that continuously triggers specific events and executes specific programs at specific frequencies. In the process of developing a Windows Store app, you can use the DispatcherTimer class that is defined in the Windows::ui::xaml namespace to create a timer. The DispatcherTimer class contains the following members:

    • The Tick event, the event that is triggered periodically.
    • The start function is used to start the timer.
    • The Stop function, which is used to stop the timer.
    • The Interval property sets the time period for which the tick event is triggered, and the type of this property value is TimeSpan.

After a brief introduction of the DispatcherTimer class, the next simulation implements a simple timer. In Visual Staudio 2012, create a blank application project for the Windows Store in Visual C + +, named Dispatchertimerdemo, Next, add the following code to the grid element of the MainPage.xaml file to layout the foreground interface.

<stackpanel horizontalalignment= "Center" margin= "50,300,0,0" >

<textblock x:name= "Clocktext" fontsize= "></TextBlock>"

<Grid>

<Grid.ColumnDefinitions>

<columndefinition width= "Auto" ></ColumnDefinition>

<columndefinition width= "*" ></ColumnDefinition>

</Grid.ColumnDefinitions>

<button x:name= "Start" click= "Startclick" content= " Start "grid.column=" 0 "></Button>

<button x:name= "Stop" click= "Stopclick" content= " Stop "grid.column=" 1 "></Button>

</Grid>

</StackPanel>

In the above code, a TextBlock control and two buttons are added. Name the TextBlock control Clocktext to show the timing of the timer. The two buttons are the Start button and the Stop button, where the Start button is used to start the timer and the Stop button to stop the timer.

After the foreground interface is laid out, the next step is to add the timer's background implementation code. Open the Mainpage.xaml.h header file and add the following code:

Private

???? declaring a dispatchertimer type variable timer

???? Windows::ui::xaml::D ispatchertimer^ timer;

???? declaring a TimeSpan type variable TimeSpan

???? Windows::foundation::timespan TimeSpan;

???? declaring a int32 type variable

???? Int32 highnum;

???? declaring a int32 type variable

???? Int32 lownum;

In the preceding code, four private member variables were declared with the Private keyword, timer, TimeSpan, Highnum, and Lownum, where the timer is a dispatchertimer-type variable that represents a timer, A timespan is a variable of type timespan that is used to represent time. Both Highnum and lownum are variables of type int32, each representing the 10-digit and single-digit timers.

After declaring the above variables, open the MainPage.xaml.cpp source file and add the following code to the constructor:

Mainpage::mainpage ()

{

???? InitializeComponent ();

???? to Create An object of the DispatcherTimer class

???? Timer=ref new DispatcherTimer ();

???? Add an event function for the Tick Event

???? Timer->tick +=ref New Eventhandler<object^> (this,&dispatchertimerdemo::mainpage::D Ispatchertimertick );

???? Duration attribute record time is 1s

???? timespan.duration=10000000;

???? Set the time interval

???? timer->interval=timespan;

???? Highnum Variable assignment value 0

???? highnum=0;

???? Lownum Variable assignment value 0

???? lownum=0;

}

In the preceding code, initialize an object timer for the DispatcherTimer class and add an event handler function Dispatchertimertick for the timer object's Tick event, The specific implementation code for the Dispatchertimertick function is described later. The Duration property of the timespan variable is then assigned a value of 10000000, and the timespan variable is assigned to the interval property of the timer object, so that the timer object's tick event is triggered every 1 seconds. Finally, the Highnum variable and the lownum variable are assigned a value of 0, which is used to indicate the start time of the timer.

Before implementing the Dispatchertimertick function, you first need to declare it in the Mainpage.xaml.h header file, as shown in the following code:

Public

???? Update Timer Timings

???? void Dispatchertimertick (object^ sender, object^ e);

In the preceding code, use the Public keyword to declare a common Dispatchertimertick function that updates the timer's timing and displays the updated timings to the foreground interface.

After declaring the Dispatchertimertick function, next add the implementation code for the Dispatchertimertick function in the MainPage.xaml.cpp source file, as shown in the following code:

// Update Timer Timings

void Dispatchertimerdemo::mainpage::D ispatchertimertick (object^ sender, object^ e)

{

???? when lownum is less than 9 ,lownum increases by 1

???? if (lownum<9)

???? {

???????? lownum++;

????}

???? Else

???? {

???????? when lownum is greater than 9 , lownum is set to 0

???????? lownum=0;

???????? Highnum is less than 9 ,highnum increases by 1

???????? if (highnum<9)

???????? {

???????????? highnum++;

????????}

???????? Else

???????? {

???????????? Highnum is greater than 9 , highnum is set to 0

???????????? highnum=0;

????????}

????}

???? Display Timings to the TextBlock Control

???? clocktext->text= " start timing:" +highnum+lownum;

}

In the above code, when the value of the Lownum variable is less than 9 o'clock, the lownum variable is self-increment by 1. When the value of the Lownum variable is greater than 9 o'clock, the Lownum variable is assigned a value of 0 and the value of the Highnum variable is set. Similarly, when the value of the Highnum variable is less than 9 o'clock, the highnum variable is self-increment by 1. When the Highnum variable is greater than 9 o'clock, the Highnum variable is assigned a value of 0. Finally, the Highnum variable and the Lownum variable are assigned to the Text property of the TextBlock control named "Clocktext" to display the timings to the foreground interface.

After you have added the implementation code for the Dispatchertimertick function, next add the Click event handler Startclick for the Start button. Add the following code to the Mainpage.xaml.h header file to declare the Startclick function.

Public

???? Start Timer

???? void Startclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e);

After declaring the Startclick function, next add the implementation code for the Startclick function in the MainPage.xaml.cpp source file, which calls the Timer object's start function to start the timer. The specific code looks like this:

// Start Timer

void Dispatchertimerdemo::mainpage::startclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e)

{

???? Timer->start ();

}

Then add the Click event handler Stopclick to the Stop button and add the following code to the Mainpage.xaml.h header file to declare the Stopclick function.

Public

???? Stop Timer

???? void Stopclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e);

After declaring the Stopclick function, next add the implementation code for the Stopclick function in the MainPage.xaml.cpp source file, which calls the Timer object's stop function to stop the timer. The specific code looks like this:

// Stop Timer

void Dispatchertimerdemo::mainpage::stopclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e)

{

???? Timer->stop ();

}

After you run the Dispatchertimerdemo project, click the Start button to start the timer, which displays the timer interface shown in 20-1.

Figure 20-1 Timer

WIN10 Series: VC + + Timer

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.