简体   繁体   中英

How to get a correct time interval in windows universal when the app is not in foreground?

I need to check whether the app was opened less than 15 minutes ago or not. If I choose a background task for this, windows does not guarantee that the task will not be killed. Is there any way to get a time stamp independent of the system time? Like in android, we can get time since boot which can be easily used here. I'm developing a win 10 app(C#).

I suggest you use a DispatcherTimer class, which is well documented on MSDN with a great example. You create a DateTimeOffset startTime; as a global variable. Then, you create a Tick event at every 30 seconds to verify the elapsed time :

DateTimeOffset time = DateTimeOffset.Now;
TimeSpan span = time - startTime;

All the details to create the Tick events are well documented on the link I provided for DispatcherTimer .

TickCount is what I used to solve this problem. This gives the time in milliseconds since boot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM