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
  • 2
  • 2
LVL 22

Expert Comment

Comment Utility
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

Comment Utility
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
Comment Utility
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
Comment Utility

   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

Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights  challenge 17 59
mapShare challenge 13 66
wordlen challenge 3 46
oracle query help 18 72
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now