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 :(

C#
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.
11,335 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,804 questions
{count} votes

3 answers

Sort by: Most helpful
  1. KOZ6.0 6,580 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);
    }
    
    

  2. Dewayne Basnett 1,371 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
    
    
    0 comments No comments

  3. Dewayne Basnett 1,371 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
    
    
    0 comments No comments

Your answer

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