Solved

Is Service Installed Hangup?

Posted on 2010-11-09
11
274 Views
Last Modified: 2012-05-10

The following "IsServiceInstalled" method looks good, but it throws errors when cannot find a service.

I don't understand why it doesn't simply return "false" if a service is not found.

I use this code to call the following method;

        if (IsServiceInstalled(control.ServiceName))                    
        //run other code

        private bool IsServiceInstalled(string svcName)
        {
            ServiceController[] services = ServiceController.GetServices();

            foreach (ServiceController service in services)
            {
                if (service.ServiceName == svcName)
                    return true;
            }
            return false;
        }

       
0
Comment
Question by:pointeman
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34097648
I'm guessing that "ServiceController.GetServices()" is what is causing the exception. Even though you can enumerate the running services on a system, I have run into issues where certain processes cannot be accessed because of permissions. I would suggest putting a try/catch inside your foreach and/or elevating your program. I'm not completely sure the latter will do the trick.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34097658
Correction:

>>  I'm guessing that "ServiceController.GetServices()" is what is causing

I'm guessing that "service.ServiceName == svcName" is what is causing...
0
 
LVL 5

Expert Comment

by:KenIBrown2
ID: 34097660
Step through the code in debugger to find which actual line throws the error.
0
 

Author Comment

by:pointeman
ID: 34097669
The more I think about it, I wonder if I'm not waiting long enough for a result. I know the list of services on any pc would be long.
Here's the method I use to call each servicecontroller:

foreach (ServiceController control in controllers)
{
       if (IsServiceInstalled(control.ServiceName))
       {
             ServiceCommand(control, (string)command);
       }
       Thread.Sleep(5000);
}  
 
0
 
LVL 13

Expert Comment

by:iHadi
ID: 34097695
The code runs smoothly without any exceptions and returns false if not found. Can you post the exception you are getting and the stack trace
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:pointeman
ID: 34097698
Doing this, but not any better:
try
{
       private bool IsServiceInstalled(string svcName)
        {
            ServiceController[] services = ServiceController.GetServices();

            foreach (ServiceController service in services)
            {
                if (service.ServiceName == svcName)
                    return true;
            }            
}
catch
{
     return false;
}
  return true;
}

 
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34097727
That's not even valid code. You can't surround a function definition with a try/catch. What I was referring to was more like:
private bool IsServiceInstalled(string svcName)
{
    ServiceController[] services = ServiceController.GetServices();

    foreach (ServiceController service in services)
    {
        try
        {
            if (service.ServiceName == svcName)
                return true;
        }
        catch
        {
            // One failed
        }
    }
} 

Open in new window

0
 

Author Comment

by:pointeman
ID: 34097796
Good greif, isn't there a better way to look for a service exists before calling ServceControllers?
0
 
LVL 5

Expert Comment

by:KenIBrown2
ID: 34097828
In your new sleep 5s method - when does it error? immediately, or after some time - that would surely indicate where roughly the problem is....
0
 
LVL 5

Expert Comment

by:KenIBrown2
ID: 34097900
I missed a few posts, but surely the try/catch is just a temporary measure to narrow down what is causing the problem so you can handle it more nicely; not an actual solution. Is there any reason you cant debug it as my suggestion?
0
 

Author Comment

by:pointeman
ID: 34102735
Using this to solve problem, thanks....  

       private bool IsServiceInstalled(string svcName)
       {
           ServiceController[] services = ServiceController.GetServices();

           foreach (ServiceController service in services)
           {
                try
                {
                      if (service.ServiceName == svcName)
                             return true;
                }
                catch
                {
                     return false;
                }
               return false;
       }

 
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now