Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using GetProcessesByName the best way to check if a process is running?

Even though the topic title explains most of the question, I'd like to sketch out the scenario so you understand in what context this question is put.

I have an application which is like an Outlook contacts list. It gets all the contacts from Outlook and the result is that they're displayed in a data grid view. Now, instead of starting Outlook every time my application opens and shutting it down, I want it not to open when it is already open and stay open when my application shuts down and the user already had Outlook running. Here's my usage:

Process[] pName = Process.GetProcessesByName("OUTLOOK");
if (pName.Length == 0)
{
    MessageBox.Show("Outlook is not running."); // Open Outlook anew.
}
else
{
    MessageBox.Show("Outlook is running."); // Do not re-open Outlook.
}

Is this the best and safest way of doing it? Thank you in advance.

like image 357
Fusyion Avatar asked Jun 23 '10 08:06

Fusyion


People also ask

How do you check if another instance of the application is running?

Count() > 1; This works for any application (any name) and will become true if there is another instance running of the same application.

How do you end a process in C#?

Type the program name you want to close with it's extension (.exe). Some applications cannot be stopped without forcing. Use /F after taskkill to force the action. If you want to close the process tree use /T after program name.

How do I start a process in C#?

C# Process run program Diagnostics; using var process = new Process(); process. StartInfo. FileName = "notepad.exe"; process. Start();


1 Answers

Yes. I can think of no better way.

like image 129
Pavel Radzivilovsky Avatar answered Sep 17 '22 13:09

Pavel Radzivilovsky