0

I'm writing a button_click function in c#, in which i use the executeCommand to talk to a service.

ServiceController sc = new ServiceController("MyService");
int command = 145;
sc.Stop();
//writing xml file
sc.Start();
sc.ExecuteCommand(command);

At some point the program is crashing, is not able to call the executecommand because the service is in a start/stop pending status. What do i do? I need to stop/start the service because the function has to write the same xml file.

dasy
  • 49
  • 1
  • 5

1 Answers1

0

Add a try-catch-finally block, see if there is some exception that is causing the service to crash.

try{
     ServiceController sc = new ServiceController("MyService");
     int command = 145;
     sc.Stop();
     //writing xml file
     sc.Start();
}
catch(Exception ex)
{
     throw ex;
}
finally{
     sc.ExecuteCommand(command);
}
Aishwarya Shiva
  • 3,460
  • 15
  • 58
  • 107