Windowscommunitytoolkit: SystemInformation.AppUptime maybe not a precise and reliable

Created on 21 Aug 2019  路  7Comments  路  Source: windows-toolkit/WindowsCommunityToolkit

Describe the bug

Since SystemInformation.AppUptime is a variable to record how long the app has been running, so the total should take the window minimized time into account.
In currently version, suppose I minimized the app when it has been running 1 minute, after a long time, I restore the app. The SystemInformation.AppUptime is still 1 minute.....

As I read the code

public static TimeSpan AppUptime
        {
            get
            {
                if (LaunchCount > 0)
                {
                    var subsessionLength = DateTime.UtcNow.Subtract(_sessionStart).Ticks;

                    var uptimeSoFar = _localObjectStorageHelper.Read<long>(nameof(AppUptime));

                    return new TimeSpan(uptimeSoFar + subsessionLength);
                }
                else
                {
                    return TimeSpan.MinValue;
                }
            }
        }

SystemInformation.AppUptime only read variable _sessionStart, and use DateTime.UtcNow.Subtract. So If I start app, modify the system time, then SystemInformation.AppUptime is also wrong.

So is there a precise time to count how long the app is running? Thx.

bug help wanted question

All 7 comments

@hupo376787 I don't think we'd try to manage the user changing their system time.

Could you elaborate on your scenario about what you're trying to accomplish?

Thanks!

Hi, I have a solution, which means a timer start count on app launch. This is reliable, but may use a little more cpu resource. What do you think, or any other better workaround?

Thx.

This issue has been marked as "Needs: Attention 馃憢" due to no activity for 7 days. Please triage and assign the issue so the fix can be established.

@hupo376787 think about this more, why can't we just record the timestamp of when the app is launched?

Then when you request a new property like TimeSinceAppStart it can just do a calculation from the current time to that stored value? And no timers involved.

Thoughts?

Hi, @michael-hawker when getting the current time, the user may change their system time.
This result the time not accurate.

Yeah, I know, but I don't think the user changing the system time is that common of a scenario.

I mean we could provide both options, or maybe a sample in the docs that explains how to create a timer, that way the developer can decide if they want it running all the time or not if they need that level of detail?

A StopWatch may solves this issue, with very few sytem resource, no matter user change system time or not.

Was this page helpful?
0 / 5 - 0 ratings