James May
James May

Reputation:

C# Waiting for other services to start

I'm writing a Windows service that relies on other services, how should I wait for the other services to start?

Thanks

Upvotes: 3

Views: 3489

Answers (6)

Jonathan
Jonathan

Reputation: 12015

I think you shoud this line

installer.ServicesDependedOn = new string [] { "DependenceService" };

like this:

using (ServiceProcessInstaller processInstaller = new ServiceProcessInstaller())
{
    processInstaller.Account = ServiceAccount.LocalSystem;
    processInstaller.Username = null;
    processInstaller.Password = null; 

    using (ServiceInstaller installer = new ServiceInstaller())
    {
        installer.DisplayName = "yourservice.";
        installer.StartType = ServiceStartMode.Automatic;
        installer.ServiceName = "YourService";

        installer.ServicesDependedOn = new string [] { "DependenceService" };
        this.Installers.Add(processInstaller);
        this.Installers.Add(installer);
    }
}

good luck

Upvotes: 4

Alfred Myers
Alfred Myers

Reputation: 6453

As others here said, you should use the ServiceInstaller Class, but you don't need a full blowned setup project. You can do a quick instalation using InstallUtil.exe, a command-line utility that comes with the .NET Framework.

Upvotes: 1

JP Alioto
JP Alioto

Reputation: 45127

In your service project, add a project installer as described here. One of the properties of your ProjectInstaller will be ServicesDependedOn. When you add services to that array of strings (you can do that through the IDE), they will be required to be started before your service will start. If they are not started the SCM will try and start them.

Upvotes: 0

Kim Major
Kim Major

Reputation: 3711

In addition to what other answers have alredy pointed out, if one of those services is SQL Server you will need to ensure that the specific database is available as well as the SQL Server service itself. I use a function similar to the following:

public class DbStatus
{
    public static bool DbOnline()
    {
        const int MaxRetries = 10;
        int count = 0;

        while (count < MaxRetries)
        {
            try
            {
                // Just access the database. any cheap query is ok since we don't care about the result.
                return true;
            }
            catch (Exception ex)
            {
                Thread.Sleep(30000);
                count++;
            }
        }
        return false;
    }
}

Upvotes: 3

devdimi
devdimi

Reputation: 2462

Do you have control over the other services? If yes have them start you, if not I guess you have to start anyway and monitor yourself what is going on. It is possible to register yourself with WMI to get notifyied when other processes are started - there is a question about it.

Upvotes: 0

Adam Robinson
Adam Robinson

Reputation: 185663

You need to specify the dependencies. You can do this in your Installer class.

Further clarification

You should be using an Installer class as a Custom Action in your setup project to install your service. If you aren't, post a comment and I'll update this answer with steps on how to do that.

Inside the designer for your Installer class you should see two components: a serviceInstaller and a serviceProcessInstaller. I don't remember which off of the top of my head, but one of these has a property that allows you to specify a multiline string that lists the service names of your service's dependencies.

Upvotes: 1

Related Questions