0

From a C# service, how I can check whether another app is dead or not?

I tried to use Process.Responding, it returns true but the app is died.

enter image description here

This is the code:

private List<string> getListStringGAppPath()
    {
        List<string> listGAppPaths = new List<string>();
        Process[] processes = Process.GetProcessesByName("MyApp");

        if (processes.Length > 0)
        {
            for (int i = 0; i < processes.Length; i++) {
                listGAppPaths.Add(processes[i].Responding.ToString() + "@@@@@@" + processes[i].MainModule.FileName);
                //processes[i].Responding.ToString() always return True
            }
            return listGAppPaths;
        }
        else
        {
            return null;
        }
    }
jewelnguyen8
  • 249
  • 3
  • 16

3 Answers3

0

When process dies, windows seems to toggles its state to Suspended, you can try checking its state first. Also here: Detecting process crash in .NET

BladeMight
  • 2,670
  • 2
  • 21
  • 35
0

You can use the methods in System.Diagnostics.Process to get process information.

GetProcessesByName(String)

Creates an array of new Process components and associates them with all the process resources on the local computer that share the specified process name.

GetProcessById(Int32)

Returns a new Process component, given the identifier of a process on the local computer.

GetProcesses()

Creates a new Process component for each process resource on the local computer.

If the process does not exist, then it must have died?

demo
  • 6,038
  • 19
  • 75
  • 149
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
0

You can check if the process is responding:

foreach (var process in System.Diagnostics.Process.GetProcesses())
{
    Console.WriteLine("Process Name: {0}, Responding: {1}", process.ProcessName, process.Responding);
}

Similar to this answer: Check status of process