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#).
Asked
Active
Viewed 200 times
0
-
what task you want to run after 15 minute? – Irfan May 12 '16 at 11:19
-
I want to check whether a flag(stored in database) is true or not. – sant May 12 '16 at 11:50
-
1Why not to save the current time in app's settings and then upon resuming/launching check current time and compare with saved value? – Romasz May 12 '16 at 14:05
2 Answers
0
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
.

DomDev
- 540
- 2
- 12
-
Are you sure I can use this? My app may not be in the foreground and I do not want to take the local time as reference because someone may change it manually. – sant May 12 '16 at 11:53
-
I am pretty sure it works even if the app is not on foreground, but you can test it by using smaller intervals (10 seconds instead of 30, and 1 minute instead of 15). Also, you can detect if the person changed the local time simply by verifying if the current time is smaller than the time of the event 30 seconds ago. – DomDev May 12 '16 at 12:15
-
I just tested with my app! The dispatcher timer and the events still work in background! The problem you mentioned is true, that one can change the PC time and that it will impact the DispatcherTimer accordingly. However, you can only change the minutes on your PC, so if you do an event that checks the time at every 15 seconds, then you can easily detect if the person cheated the time. – DomDev May 12 '16 at 12:23
-
Ok, I just understood that you need to check the time even if the app is closed! This is different. check this site "https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task", along with the previous documentation on DispatcherTimer. – DomDev May 12 '16 at 12:27
-
NO - this won't work on mobiles - your app will be suspended quickly once it goes to background - in debug it will work, hence [lifecycle events are not triggered automatically](http://stackoverflow.com/q/24103101/2681948). – Romasz May 12 '16 at 14:04
-
What you could do is to save the time in a file, and then read it next time it is opened. Now people could change their phone time, but only a tiny minority would do this to save 15 minutes. – DomDev May 12 '16 at 14:25
-
My objective is to lock my app every 15 min if the user has not entered into it. When users unlocks or enters into app before 15 min, I register that timestamp. So, I cannot keep system time as reference. My problem will be solved once I have a different reference, which any user cannot change. – sant May 13 '16 at 05:37
-
Background Task may not serve my purpose since the OS does not guarantee its activation. I am new to this, please tell me if I understood something wrong. – sant May 13 '16 at 05:40
-
I think I can just use the [TickCount](https://msdn.microsoft.com/en-us/library/system.environment.tickcount(v=vs.110).aspx) for this problem. Thank you guys. – sant May 13 '16 at 08:52