Does a C# app track how long its been running?

C#.Net

C# Problem Overview


And if it does, is there an easy way to get the total time since it started?

C# Solutions


Solution 1 - C#

The System.Diagnostics.Process class has a property containing the start time which you can use to calculate how long it has been running:

var current = System.Diagnostics.Process.GetCurrentProcess();
DateTime startedAt = current.StartTime

Solution 2 - C#

Use StopWatch class for this feature.

Even if quite overkill, it will always work, even if the user changes the clock or even if there is some daylight saving changes during the process. - Julien Lebosquain (Comment to my answer.)

Solution 3 - C#

You can Have a Static DateTime Variable for your Project, and store the datetime in the Constructor of your Main Class. Referencing this Static Variable, and comparing with current time helps you achieve your result

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbrokeView Question on Stackoverflow
Solution 1 - C#LeeView Answer on Stackoverflow
Solution 2 - C#Nikhil AgrawalView Answer on Stackoverflow
Solution 3 - C#AkhilView Answer on Stackoverflow