Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to determine how long a process has been running

Tags:

c#

process

Is it possible, in C#, to get a list of running processes (not service processes, but actual applications) and get a DateTime of when the application started? Or a TimeSpan or even an integer of how long a process has been running?

like image 713
Icemanind Avatar asked Aug 04 '13 02:08

Icemanind


People also ask

How do I find long running processes in Unix?

Type the ps aux command to see all running process in Unix. Alternatively, you can issue the top command to view running process in Unix.

How can you tell how old a process is in Linux?

If you want to figure out how long a process has been running in Linux for some reason. We can easily check with the help of “ps” command. It shows, the given process uptime in the form of [[DD-]hh:]mm:ss, in seconds, and exact start date and time. There are multiple options are available in ps command to check this.

How long a system has been running Linux command?

First, open the terminal window and then type: uptime command – Tell how long the Linux system has been running. w command – Show who is logged on and what they are doing including the uptime of a Linux box. top command – Display Linux server processes and display system Uptime in Linux too.


1 Answers

Process.GetProcesses will retrieve a list of running processes.

Each Process has a StartTime property that

Gets the time that the associated process was started.

Simply subtract that from DateTime.Now to get how long the process has been running.

static void Main(string[] args)
{
    var procs = Process.GetProcesses();
    foreach (var proc in procs) {
        TimeSpan runtime;
        try {
            runtime = DateTime.Now - proc.StartTime;
        }
        catch (Win32Exception ex) {
            // Ignore processes that give "access denied" error.
            if (ex.NativeErrorCode == 5)
                continue;   
            throw;
        }

        Console.WriteLine("{0}  {1}", proc, runtime);
    }

    Console.ReadLine();
}
like image 158
Jonathon Reinhart Avatar answered Sep 28 '22 02:09

Jonathon Reinhart