How can I get the running time of a process in WPF?

In WPF, you can utilize the Process class to obtain the running time of a process. The specific steps are as follows:

  1. System.Diagnostics can be reformulated as “Diagnostic System”.
  2. Retrieve the current process.
  3. Procedure
  4. The starting time of the process.
  5. The current date and time
  6. Calculate the running time of the process as the current time minus the start time.

Here is an example code:

using System;
using System.Diagnostics;

namespace WpfApp
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            // 获取当前进程
            Process currentProcess = Process.GetCurrentProcess();

            // 获取进程的启动时间
            DateTime startTime = currentProcess.StartTime;

            // 获取当前时间
            DateTime currentTime = DateTime.Now;

            // 计算进程的运行时间
            TimeSpan runningTime = currentTime - startTime;

            // 输出进程运行时间
            Console.WriteLine("进程运行时间:" + runningTime);
        }
    }
}

Please note that the Console.WriteLine method in the above sample code is used to output the process running time to the console. You can replace it with other operations according to your needs, such as displaying it in a window.

bannerAds