Priyan Rajeevan
Priyan Rajeevan

Reputation: 1052

Restart Windows Service From Service

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.

Upvotes: 0

Views: 531

Answers (1)

Vladimir Shiyanov
Vladimir Shiyanov

Reputation: 1396

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.

Upvotes: 0

Related Questions