5

I created a windows service project. And if you create a new project you get something like this:

ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] 
{ 
  new Service1() 
};
ServiceBase.Run(ServicesToRun);

And now I have to add some functions and timer to my class Service1() and then everything is fine.

Now let say, my service should do some stuff like: reading some files, deleting some folders, checking connections...

And all of them should run async. What is now better? Copy all of the functiont in Service1() and not changing anything else or to create for every "stuff" a new class (Service1(), Service2(), ...) and add them like

ServicesToRun = new ServiceBase[] 
{ 
  new Service1(),
  new Service2() //not sure this will compile
};

Just asking. Maybe I did not understand 100% how services are working...

silla
  • 1,257
  • 4
  • 15
  • 32
  • Interesting question. I've wondered that but never tried it. Oh, it'll certainly *compile* - the question really is: does it *work*. Given the API accepts an array, my default assumption is "yes, that'll work fine". But ... Haven't tried it. Of course, since the service needs to start a new thread anyway, you could just use a single service instance to start 3 sets of operations, – Marc Gravell Sep 08 '12 at 21:51
  • Did you see this? http://stackoverflow.com/questions/1688275/can-i-have-multiple-services-hosted-in-a-single-windows-executable -- which discusses how but not the why. It really is a design question and depends on what you are doing. – Donald Byrd Sep 09 '12 at 01:51

1 Answers1

0

This may be a bit too general for the question but a Windows service should perform a single or group of related functions. In general, the fewer services the better but, once the service starts to do multiple things, consider separating them if:

  1. They require different levels of security. For example, if certain functions need administrator access and some don't, consider splitting them into different services.
  2. A user could or would want to one feature running (i.e. service) and not the other. For example, certain functionality may depend on an optional component or one that may not be installed.
akton
  • 14,148
  • 3
  • 43
  • 47