Skip to main content

reshefm had a pretty nice answer; however, it does not account for a situation in which the process was never started to begin with.

Here is a a modified version of what he posted.

 public static bool IsRunning(this Process process) {  {try {  try { Process.GetProcessById(process.Id).Dispose();} }  catch (InvalidOperationExceptionException e) {when return(e false;is }ArgumentException or InvalidOperationException) {  catch (ArgumentException){ return false;} }  return true;  } 

I removed his ArgumentNullException because its actually suppose to be a null reference exception and it gets thrown by the system anyway and I also accounted for the situation in which the process was never started to begin with or the close() method was used to close the process.

reshefm had a pretty nice answer; however, it does not account for a situation in which the process was never started to begin with.

Here is a a modified version of what he posted.

 public static bool IsRunning(this Process process) { try {Process.GetProcessById(process.Id);} catch (InvalidOperationException) { return false; } catch (ArgumentException){return false;} return true;  } 

I removed his ArgumentNullException because its actually suppose to be a null reference exception and it gets thrown by the system anyway and I also accounted for the situation in which the process was never started to begin with or the close() method was used to close the process.

reshefm had a pretty nice answer; however, it does not account for a situation in which the process was never started to begin with.

Here is a a modified version of what he posted.

public static bool IsRunning(this Process process) {  try {   Process.GetProcessById(process.Id).Dispose(); }  catch (Exception e) when (e is ArgumentException or InvalidOperationException) {   return false; }  return true; } 

I removed his ArgumentNullException because its actually suppose to be a null reference exception and it gets thrown by the system anyway and I also accounted for the situation in which the process was never started to begin with or the close() method was used to close the process.

Source Link
Aelphaeis
  • 2.6k
  • 3
  • 26
  • 42

reshefm had a pretty nice answer; however, it does not account for a situation in which the process was never started to begin with.

Here is a a modified version of what he posted.

 public static bool IsRunning(this Process process) { try {Process.GetProcessById(process.Id);} catch (InvalidOperationException) { return false; } catch (ArgumentException){return false;} return true; } 

I removed his ArgumentNullException because its actually suppose to be a null reference exception and it gets thrown by the system anyway and I also accounted for the situation in which the process was never started to begin with or the close() method was used to close the process.