Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Running multiple windows services

Posted on 2004-08-04
Medium Priority
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 1400 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 600 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …
Introduction to Processes

636 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