Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,


I want to develop a c# program to check whether a process in my system is running or not and it should display a message box whether the process is running or not running.

I have tried the below code, but unfortunately it is showing as process not running always even when the process is running.

C#
Process[] pname = Process.GetProcessesByName("VLC Player");
if (pname.Length > 0)
{
MessageBox.Show("Process Running");
}
else
{
MessageBox.Show("Process Not running");
}
System.Threading.Thread.Sleep(5 * 1000);


But always it is showing as Process not running, It is by default taking the pname.Length value as Zero. Please give me some valuable suggestions.
Posted
Comments
Sergey Alexandrovich Kryukov 27-Jan-16 11:48am    
If a process is "in my system", in what sense it could be "not running"?
And "using C#" is pure gibberish. There is no such thing during process runtime (unless this is about C# code compilation). During runtime, all code is either in IL (at first, before JIT compiles it all, method by method) or native.

So, the question makes no sense at all. And the code sample shown makes even less sense. By the way, "processes by name" does not guarantee that you get the process you are looking for. But this problem is solvable...

Take all processes, and you will see that your process is actually running (if it is really running). Most likely, you just use wrong process name.

—SA
phil.o 27-Jan-16 12:00pm    
I think the process name has to have the '.exe' extension. You may try with that.

1 solution

Probably, it's not the right name.
On my system, it's "VLC media player (32 bit)", but that doesn't find it either.
What I did was use the debugger:
C#
Process[] pname = Process.GetProcesses();
And a quick look through the list gave me:
C#
Process[] pname = Process.GetProcessesByName("vlc");
if (pname.Length > 0)
    {
    MessageBox.Show("Process Running");
    }
else
    {
    MessageBox.Show("Process Not running");
    }
Giving the right message box.
 
Share this answer
 
Comments
Richard Deeming 27-Jan-16 12:14pm    
GetProcessesByName[^] uses the ProcessName to match the process.

Looking at the remarks on the ProcessName property[^], this is the "executable file name, such as Outlook, that does not include the .exe extension or the path".

That matches with your findings - the VLC executable is called "vlc.exe", so the process name is "vlc". :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900