0

Is there any way to restart a windows service from the same service , as Application.Restart() in Windows forms, I don't want to launch another process from the service to restart the service.

1

1 Answer 1

0

You also can add Custom Action to Commit folder of Custom Actions in your setup project. It must be a primary output of class library project with class inherited from System.Configuration.Install.Installer with [RunInstaller(true)] attribute. In this class you need to override one base method:

    public override void Commit(IDictionary savedState)
    {
        base.Commit(savedState);
        ProcessStartInfo psi = new ProcessStartInfo("sc", "failure \"You service name\" reset= 60 actions= restart/1000");
        psi.CreateNoWindow = true;
        Process proc = Process.Start(psi);
        proc.WaitForExit();
    }

It's configuring your service to restart automaticaly after failure.

Than when you need to restart your service you can do

Environment.FailFast("Self restarting service...");

But it has one drawback - it will be fired an error message in event log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.