Use timer in Metro app

Source: Internet
Author: User

 

After the set time is over, execute the corresponding method.
Use the threadpooltimer class in the windows. system. Threading namespace of Metro to create a timer.
Createtimer
Createperiodictimer creates a periodic timer. The timer created by this method is similar to the timer in VC.

C # The Code is as follows:
1. Add timer-related namespace
Using Windows. UI. core;
Using Windows. system. Threading;

Namespace Timer
{
Partial class mainpage
{
Private threadpooltimer delaytimer;
Private coredispatcher sampledispatcher;
Private timerelapsedhandler timerhandle;
Private timespan timespace;

Public mainpage ()
{
Initializecomponent ();

// Responsible for processing the window messages and dispatching the events to the client.
Sampledispatcher = Window. Current. corewindow. Dispatcher;
}

Private void button_click (Object sender, routedeventargs E)
{
Timerhandle = new timerelapsedhandler (mytimer );
Timespace = timespan. frommilliseconds (5000); // set the timer time (MS)

Delaytimer = threadpooltimer. createtimer (timerhandle, timespace );

}

Private void mytimer (threadpooltimer timer)
{
Sampledispatcher. invokeasync (coredispatcherpriority. Normal,
(Sender, argS) =>
{
// Add your code
...
},
Timer,
Null );
}


}
}

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.