HasExited() generates an Access Denied error

J

John

I have a C# program that relies on a service (let's call it myService.exe).
So I wrote a routine that makes sure the service is running:

int iProcessID = IsProcessRunning("myService.exe");
Process myProcess = Process.GetProcessById(iProcessID);

These commands work great (I can post the IsProcessRunning() code if
necessary, but since that's not the issue I thought I'd leave it out).
Here's where the code fails: I check to make sure the process is alive using
this code

while(!bShutdown)
{
myProcess.WaitForExit(500);
if(myProcess.HasExited)
{
break;
}
else
{
// Periodic functions go here...
}
}

On Windows XP the code works great. On Windows Vista the .HasExited property
causes an Access Denied error. I'm sure it has something to do with
permissions, but I haven't figured it out yet. Can anyone help?

Thanks,
John
 
J

Josip Medved

To control whether service is running or not, you may want to try

System.ServiceProcess.ServiceController sc =
new System.ServiceProcess.ServiceController("MYSERVICE");
if (sc.Status == System.ServiceProcess.ServiceControllerStatus.Running) {
....
 
J

John

Josip,

Thanks for the quick reply! I tried it and it works great. I can't thank you
enough for showing me the right path.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top