开发者

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

开发者 https://www.devze.com 2023-01-04 05:53 出处:网络
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.

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)
开发者_JS百科{
    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.


Yes. I can think of no better way.

0

精彩评论

暂无评论...
验证码 换一张
取 消