1

I came across this question, which looked like it would resolve what I'm trying to do, and I'm trying to use similar code where a Process() object is created and the "sc" command is called from code.

    static void SetRecoveryOptions(string serviceName)
    {
        int exitCode;
        using (var process = new Process())
        {
            var startInfo = process.StartInfo;
            startInfo.FileName = "sc";
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;

            // tell Windows that the service should restart if it fails
            startInfo.Arguments = string.Format("failure \"{0}\" reset= 0 actions= restart/60000", serviceName);

            process.Start();
            process.WaitForExit();

            exitCode = process.ExitCode;
        }

        if (exitCode != 0)
            throw new InvalidOperationException();
    }

I've tried calling that code from a few locations (such as the committed event handler for the service installer, OnStart of the service itself, etc) but every time I get an exception as soon as the Process() object is created. The exception is: "operation is not allowed due to the current state of the object".

Any ideas what I'm missing here?

Community
  • 1
  • 1
  • Just want to be clear... is it blowing up on `new Process()` or `process.Start()`? If it's `Start()`, there's a chance the service cannot actually take control messages. I have experienced this and required either me killing service's .exe or a rebooting the machine. Then it worked fine afterwards. – TyCobb Jan 30 '15 at 17:40
  • I believe it's new Process() actually. I debugged through that and all the members in the Process object had exceptions in them as soon as it was instantiated. The resulting error code was 1060 i think. – Modern Viking Jan 30 '15 at 18:10

0 Answers0