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 );
}
}
}