Running multiple windows services

Posted on 2004-08-04
Last Modified: 2012-05-05
I wrote a windows service in C# a while back and wanted to add another to the "MyWindowsService.exe"  My second service works correctly the problem I am having is running both at once. From what documentation I have found this code should work.

      static void Main()
                  System.ServiceProcess.ServiceBase[] ServicesToRun;
                  ServicesToRun = new System.ServiceProcess.ServiceBase[]
                   { new cservice_1(), new ccervice_2() };


And it does my first service starts runs its cycle , it is timed to run every five minutes. The second service does not start until the first completes its first cycle and there after continues to run on its own timer/cycle.
The problem is the first service never runs again.

I have created seperate timers and threads for both of my services  but am at a loss as to why this is happening.

Any advice would be greatly appreciated.


Question by:C_W3
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 22

Expert Comment

ID: 11716583
Superficially, it would appear as if they're not truly independent - something in the second one may be causing the first to fail.

Make sure AutoLog is set true in your ServiceBase, then look in the event logs for errors.

Note that the second one won't get started until the first one posts a RUNNING message.  c# does that for you when the OnStart() exits.  

One of the nasty things about the c# implementation of services is that it's easy to create them, but it hides some of the things you would otherwise have to worry about yourself.  For example, although you can cause a start timeout with a delay in InitializeComponent(), c# sends hinted STARTPENDINGs for you in ServiceStart() until that routine exits, so it's easy to have an OnStart() that runs long.

It may be interesting to see what happens if you reverse the order of them in ServiceBase[].


Author Comment

ID: 11717624
I had created an event log for each service but was just using it on creation, exit and to log when the task ran.  With that in mind  and your suggestion of of reversing the order in ServiceBase[] . I did reverse the order and also wrote to the log upon exiting InitializeComponent() and the constructors.  

What I found was it did not matter what the order was in ServiceBase[]. The first service runs does its tasks and logs when finished and when its expected to run again.
 So no hang ups on OnStart().
Then the constructor for the next service is called and logs a message,InitializeComponent() and  logs another message. One last message is logged before the constuctor hits its closing bracket, then nothing happens *poof*.

It does not matter which service is second they both fail like this.

Thank you very much for your suggestions, you seem to have a good idea of what's going on in the background  any other ideas of where to go from here?

Best Regards
LVL 22

Assisted Solution

cookre earned 350 total points
ID: 11719040
How about the System event logs?  Look for the Service Control Manager event.

What's the ending state of them in the service viewer?  (services.msc)

Given that the behavior is the same would tend to support the suspicion that the one is having an adverse effect on the other, but not seeing the source, I can't go much beyond that.

If you're pressed for time, you may want to consider just starting the one in your ServiceBase and have it start the second, or just splitting them apart entirely.

Accepted Solution

Stimphy earned 150 total points
ID: 11723343

   Try removing the timers and replacing them with threads...  Adjust the sleep time for the threads accoringly.  It may be a long shot, but its worth a try.  Although I have not attempted to duplicate the problem, it maybe that the timer in one of the apps is keeping the other from firing.. You shouldnt have this issue with threading.
Good Luck!


Author Comment

ID: 11774312
Thank you both for the info and suggestions both were very helpfull. I ended up getting rid of the thread and timer in one service and when the first completes its task it checks the date/time and fires off the second when it matches my designated time.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Six Sigma Control Plans

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question