简体   繁体   中英

Windows service that won't run multiple services

I have created a Windows Service in Visual Studio 2012 that should run 2 services when executed.

static void Main()
    {
        // creates an array to hold all services that will run in this application
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        {
            // services added here
            new Service1(),
            new Service2()
        };
        ServiceBase.Run(ServicesToRun);
    }

I have already read various possible answers in various forums, etc. but I have not found a working solution for me.

I have added a Service Installer for each service and I also have one project installer.

When I install the service through the Developer Command Prompt I can see the Windows Service in the Computer Manager but I can also see Service1 and Service2.

When I run the Windows Service, it will only run Service1 and not Service2. However, both Service1 and Service2 can be started individually.

Anyone any suggestions? Been stuck on this for some time now!

EDIT

In the Service1 and Service2 OnStart() I have the following code:

CheckService();

        try
        {
            motorTimer = new Timer();
            // timer sets intervals for when quotes are checked after start up
            motorTimer .Elapsed += new ElapsedEventHandler(QuoteTimerElapsed);
            motorTimer .Interval = Convert.ToDouble(ConfigurationSettings.AppSettings["ServiceCheckInterval"]);
            motorTimer .Start();
        }
        catch (Exception ex)
        {
            MotorServiceCheckLogDetail(ex.ToString());
            OnStop();
        }

As an answer to my problem I took the following approach. It might not be perfect and I slightly changed my approach but it was a way I was able to get my Service to work how I wanted.

Instead of creating 1 service with multiple 'services' being run within it, I created one service that instantiated various classes. I was able to include the functionality required in these classes and call the class when required.

As I say, this isn't the perfect answer to my original problem but it was a work around I discovered and as a result it may help others if they read this SO post.

I have already heard/read some problem that people are facing when they are using timer in windows service. Instead of using timer I'd recommend in OnStart method start a task that uses loop with integrated delay. something like following:

Task _task;
CancellationTokenSource _terminator;

protected override void OnStart()
{
     _terminator = new CancellationTokenSource();
     _task = Task.Factory.StartNew(TaskBody, _terminator.Token);
}

private void TaskBody(object arg)
{
     var ct = arg as CancellationToken;

     if (ct == null) throw new ArgumentException();

     while(!ct.sCancellationRequested)
     {
         YourOnTimerMethod();
         ct.WaitHandle.WaitOne(interval)
     }
}

public override void OnClose()
{
    _terminator.Cancel();
    _task.Wait();
}

I had the same problem. Seems that the OnStart method is called only on the first Service passed to ServiceBase.Run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM