pointeman
asked on
Is Service Installed Hangup?
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(contro
//run other code
private bool IsServiceInstalled(string svcName)
{
ServiceController[] services = ServiceController.GetServi
foreach (ServiceController service in services)
{
if (service.ServiceName == svcName)
return true;
}
return false;
}
I'm guessing that "ServiceController.GetServ ices()" 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.
Correction:
>> I'm guessing that "ServiceController.GetServ ices()" is what is causing
I'm guessing that "service.ServiceName == svcName" is what is causing...
>> I'm guessing that "ServiceController.GetServ
I'm guessing that "service.ServiceName == svcName" is what is causing...
Step through the code in debugger to find which actual line throws the error.
ASKER
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(contro l.ServiceN ame))
{
ServiceCommand(control, (string)command);
}
Thread.Sleep(5000);
}
Here's the method I use to call each servicecontroller:
foreach (ServiceController control in controllers)
{
if (IsServiceInstalled(contro
{
ServiceCommand(control, (string)command);
}
Thread.Sleep(5000);
}
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
ASKER
Doing this, but not any better:
try
{
private bool IsServiceInstalled(string svcName)
{
ServiceController[] services = ServiceController.GetServi ces();
foreach (ServiceController service in services)
{
if (service.ServiceName == svcName)
return true;
}
}
catch
{
return false;
}
return true;
}
try
{
private bool IsServiceInstalled(string svcName)
{
ServiceController[] services = ServiceController.GetServi
foreach (ServiceController service in services)
{
if (service.ServiceName == svcName)
return true;
}
}
catch
{
return false;
}
return true;
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Good greif, isn't there a better way to look for a service exists before calling ServceControllers?
In your new sleep 5s method - when does it error? immediately, or after some time - that would surely indicate where roughly the problem is....
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?
ASKER
Using this to solve problem, thanks....
private bool IsServiceInstalled(string svcName)
{
ServiceController[] services = ServiceController.GetServi ces();
foreach (ServiceController service in services)
{
try
{
if (service.ServiceName == svcName)
return true;
}
catch
{
return false;
}
return false;
}
private bool IsServiceInstalled(string svcName)
{
ServiceController[] services = ServiceController.GetServi
foreach (ServiceController service in services)
{
try
{
if (service.ServiceName == svcName)
return true;
}
catch
{
return false;
}
return false;
}