Share via

Get my winforms app's uptime?

Peter Volz 1,295 Reputation points
2023-09-26T05:52:51.0733333+00:00

Hello all,

Anyone aware of a method to get the total uptime of my own .net app?

Didn't find in Google :(

Developer technologies | VB
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


3 answers

Sort by: Most helpful
  1. Dewayne Basnett 1,386 Reputation points
    2023-09-26T16:49:01.06+00:00

    A fuller example.

    Public Class Form1
    
        Private runTime As Stopwatch = Stopwatch.StartNew
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'show run time
            ' Standard TimeSpan format strings
            ' https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-timespan-format-strings
    
            'Label1.Text = runTime.Elapsed.ToString("c")
    
            ' Custom TimeSpan format strings
            ' https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-timespan-format-strings
    
            '                         days hours minutes seconds milliseconds
            Label1.Text = runTime.Elapsed.ToString("d\.hh\:mm\:ss\.fff")
        End Sub
    End Class
    
    

    Was this answer helpful?

    0 comments No comments

  2. Dewayne Basnett 1,386 Reputation points
    2023-09-26T13:22:00.68+00:00

    It should be as simple as the declaration of a Stopwatch variable.

    Public Class Form1
        Private runTime As Stopwatch = Stopwatch.StartNew
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'blah
        End Sub
    End Class
    
    

    Was this answer helpful?

    0 comments No comments

  3. KOZ6.0 6,810 Reputation points
    2023-09-26T06:19:46.36+00:00

    As far as I know, no such convenience method exists You can calculate the time elapsed since the application started running by recording the start time and then computing the difference between the current time and the start time. Here's a sample code in C#:

    public static void Main()
    {
        startTime = DateTime.Now;
            ・
            ・
            ・
        TimeSpan upTime = DateTime.Now - startTime;
        Console.WriteLine("Application uptime: {0}", upTime);
    }
    
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.