Metro App中使用Timer
?? 在設定的時間結束后,執(zhí)行相應的方法。
??? 使用metro的Windows.System.Threading命名空間下的ThreadPoolTimer類,可以創(chuàng)建一個Timer。
??? CreateTimer創(chuàng)建一個使用一次的Timer
??? CreatePeriodicTimer創(chuàng)建一個Periodic的Timer,這個方法創(chuàng)建的Timer才和VC中的Timer類似。
??? C#代碼如下:
??? 1.添加Timer相關的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);??? //設定的Timer時間(毫秒)
??????????? DelayTimer = ThreadPoolTimer.CreateTimer(TimerHandle, timespace);????????????????????????????????????
??????? }
??????? private void MyTimer(ThreadPoolTimer timer)
??????? {
??????????? SampleDispatcher.InvokeAsync(CoreDispatcherPriority.Normal,
???????????????????????????? (sender, args) =>
???????????????????????????? {?????????????????????????
???????????????????????????????? //Add your code
?????????????????????????????? ?。。。
???????????????????????????? },
???????????????????????????? timer,
???????????????????????????? null);
??????? }
?????
??? }
}
???
總結
以上是生活随笔為你收集整理的Metro App中使用Timer的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Metro App中显示Toast n
- 下一篇: 命令行运行vbs脚本并传参数给vbs中的