Timer in Portable Library

后端 未结 6 886
我寻月下人不归
我寻月下人不归 2020-11-29 03:41

I can\'t find a timer in portable library / Windows Store. (Targeting .net 4.5 and Windows Store aka Metro)

Does any have an idea on how to created some kind of timi

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 03:49

    I ended up with Observable.Timer from Reactive Extensions (Rx). Rx was already included in the project, so additional reference was not an issue.

    Here is a timer that triggers every second:

    IDisposable timer = Observable.Timer(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1))
        .Subscribe(_ => /* your useful code here */);
    
    // unsubscribe/stop when timer is no longer needed
    timer.Dispose();
    

    System.Reactive.Linq.Observable class is in PCL friendly Rx-Linq NuGet package.

提交回复
热议问题