3

I want to be able to develop a windows service which is capable of running multiple instances each with different parameters. Ideally I want to be able to maintain these parameters in a browser based control panel.

I have written a control panel in C# which saves the config data to an XML file. From this I want to be able to configure the number of services to run, and what their parameters should be. I want to be able to dynamically add and remove instances of the service as required.

My questions are: 1) Is this even possible? 2) Can I start a service with specific properties, from the control panel? (Maybe by using "NET START" with command line parameters?

[Edit] I just saw something online regarding the ServiceController class; can this be used to add and remove instances of a service as well as start/stop services? [/Edit]

Thanks for any help

CompanyDroneFromSector7G
  • 4,291
  • 13
  • 54
  • 97

1 Answers1

3

Edit: My initial answer was factually wrong.

You can use command line parameters, either with NET START (which however will only accept parameters starting with a backslash) or with SC START (which will accept anything as a parameter). You cannot start a service with dynamically chosen command line parameters. Parameters can also be specified at service registration time, in which case they remain constant thereafter.

However, starting multiple instances of a service sounds like the wrong idea. There is nothing stopping you from making just one instance of the service that you configure at runtime by communicating with it (e.g. with ServiceController.ExecuteCommand), which is what you should do IMHO.

To communicate with a service, see for example How to communicate with a windows service from an application that interacts with the desktop? and How to create and communicate with a C++ Windows Service in Visual Studio 2010?

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Makes sense. However the service would be using a file system watcher, and multiple are required because there are several folders to watch and each requires different actions to be taken when a file appears. Would it be possible to watch several folders from within the same service? SO far I haven't managed to work out how! Thanks :) – CompanyDroneFromSector7G Jan 11 '12 at 12:23
  • @bukko: First off, my initial answer was wrong (I have corrected it). Second, you can of course watch several folders from within the same service (e.g. by using the code that watches one folder lots of times). – Jon Jan 11 '12 at 12:45
  • Ok, it seems that - as you say - I can add several watchers to the same service, which looks like the best solution. I found some code which adds any number of watchers to an arrayList. However, I have no idea where this code should go, as the logical place might seem to be the InitialzeComponent method, except that we aren't allowed to put code there :( – CompanyDroneFromSector7G Jan 11 '12 at 15:08