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

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 93
delphi parse string to params 3 123
Rubik's Cube Code for Effective Presentation 3 54
Auto-indent certain lines in Notepad++ 10 76
This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
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…

803 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