Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I want to make a simple program that checks for a process and if it is running a messagebox will appear. Here is what I got so far but it doesn't show the messagebox.

C#
private void Form1_Load(object sender, EventArgs e)
       {
           foreach(Process pro in Process.GetProcesses("notepad")) {

               if( pro.ProcessName.Contains("notepad"))
               {
                   MessageBox.Show("hi");
               }



Thanks for the help in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 10-May-12 16:49pm    
Please see my answer and please ask yourself: is there anything that MSDN would not tell you?
--SA

Looks a bit odd. The GetProcesses with a string parameter searches for processes on the machine passed as the parameter, see: http://msdn.microsoft.com/en-us/library/x8b2hzk8.aspx[^].

You could try Process.GetProcessesByName Method (String)[^] instead.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-May-12 16:57pm    
Absolutely. A pointless "if". Right fix, my 5.
But the code is pretty much pointless. What is there is another application named "notepad"? However, I understand that it could be some research, for learning how things work...

By the way, I answered the question itself, as you can see.
--SA
Wendelius 11-May-12 0:47am    
Thanks SA :)
MR. AngelMendez 10-May-12 17:04pm    
My solution worked I just needed to run notepad first before running my application. Thanks for the help though I will be using your references later.
Wendelius 11-May-12 0:51am    
Glad you got it working. However, you should go through the references, especially the GetProcesses :)
You can use the property System.Diagnostics.Process.HasExited. Pay attention for the exceptions this call can throw — it can help you to cover remaining situations, such as when the process has not been started, etc.:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspx[^].

In most cases, you should not check the process status but should simply make a blocking call waiting for the process to start: Process.WaitForExit. Needless to say, in this case you should work with a process in a separate thread.

Please see: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

—SA
 
Share this answer
 
Comments
Wendelius 11-May-12 0:47am    
Good advice.
Sergey Alexandrovich Kryukov 11-May-12 10:37am    
Thank you, Monjurul.
--SA

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