HasExited() generates an Access Denied error

  • Thread starter Thread starter John
  • Start date Start date
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
 
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) {
....
 
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
 
Back
Top