Running multiple windows services

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.


Who is Participating?
StimphyConnect With a Mentor Commented:

   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!

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[].

C_W3Author Commented:
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
cookreConnect With a Mentor Commented:
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.
C_W3Author Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.