Metro App中使用Timer

 

   在设定的时间结束后,执行相应的方法。
    使用metro的Windows.System.Threading命名空间下的ThreadPoolTimer类,可以创建一个Timer。
    CreateTimer创建一个使用一次的Timer
    CreatePeriodicTimer创建一个Periodic的Timer,这个方法创建的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);
        }

     
    }
}

   

你可能感兴趣的:(timer,object,Class,processing,button,events)