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

I am attempting to check that the services are running on my remote machine.
That said I have wrote some code that works great reporting that the service is running, however when I stop the service and try to do a check, the process fails with an exception message "Not Found".

C#
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, SERVICE))
                {
                    ManagementObjectCollection collection = searcher.Get();
                    foreach (ManagementObject service in collection)
                    {
                        if (service["Started"].Equals(true))
                        {
                            label.Text = "SERVICE: Running";
                        }
                        else if (service["Stopped"].Equals(true))
                        {
                            label.Text = "SERVICE: Stopped";
                        }
                        else if (service["Paused"].Equals(true))
                        {
                            label.Text = "SERVICE: Paused";
                        }
                        else if (service["Stopping"].Equals(true))
                        {
                            label.Text = "SERVICE: Stopping";
                        }
                        else if (service["Starting"].Equals(true))
                        {
                            label.Text = "SERVICE: Starting";
                        }
                        else
                        {
                            label.Text = "SERVICE: Changing";
                        }
                    }
                }


It fails on "Stopped" check. That tells me that it does detect that the service really is stopped but it fails to show me that. What am I doing wrong?
Posted
Comments
Sergey Alexandrovich Kryukov 28-Jan-15 21:22pm    
First of all, you are not showing the code causing the problem. More importantly, how can you expect something good if you are writing code in so messy manner? Look, 6 fragments repeat the same code. You should never repeat such thing; code is the same and should be re-written using a method abstracting out index and server status string. Execution depends on hard-coded immediate constants like "Starting". This is not maintainable. Write things accurately and thing on how to ask your question with relevant information, edit your post with "Improve question". See also: SSCCE.
—SA
Silver13 28-Jan-15 21:46pm    
Thank you for the quick reply to my question. I am sorry that it was not clear, as this is only an experimental code and no where near perfect. Hence me asking questions about it. I am even open to suggestions which involve scrapping the whole thing for a more efficient model if you or anyone else has a suggestion.

However, while reading your message you made me think about the problem in a different way thanks to your statement:

"More importantly, how can you expect something good if you are writing code in so messy manner? Look, 6 fragments repeat the same code. You should never repeat such thing?"

You are correct, it is messy and not at all usable at this stage. Instead you have given me an idea on how to proceed, so in a way you have answered my question. Thank you.
Sergey Alexandrovich Kryukov 28-Jan-15 21:57pm    
You are welcome. It's not the problem that it's not clear. Of course experimental code can be dirty. The problem is wasting time on dirtiness, instead of saving time on fine detail, which would be reasonable.

"Instead of giving the idea"? Idea on what?! You did not provide any information relevant to the problem you mentioned (which you also did not describe), so what else could you expect?

Do something about it, if you really could use some help.

—SA
Silver13 28-Jan-15 22:11pm    
I had to test my idea first. I have provided my solution and offered an additional from a link.
Sergey Alexandrovich Kryukov 29-Jan-15 0:28am    
Very good.
—SA

1 solution

To simply check that the service is running following IF statement is sufficient to do so:

C#
if (service["Started"].Equals(true))
{
   lbl_eid_deliver.Text = "SERVICE: Running";
}
else
{
   lbl_eid_deliver.Text = "SERVICE: Offline";
}


However, there is a another method that can be used to do same thing by using ServiceController.

An example can be seen in the link provided below.

http://stackoverflow.com/questions/178147/how-can-i-verify-if-a-windows-service-is-running
 
Share this answer
 

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