Friday, April 29, 2011

Is it possible to change values in windows service - in ini file or xml ?

Hi

I have Windows service, that have Timer.

I want the option to change the Timer speed value.

how I can do it ? with ini file ? or xml file ?

Is it possible ? if yes, can I get sample code ?

thank's in advance

From stackoverflow
  • You could have your windows service check for changes made to your config file (txt or xml) every 10 minutes for example (with a timer or a tread). If a change is detected, have your windows service reload itself with the new configuration values.

    I once made a service in C# that recorded every 10 minutes the last write time to the config file:

    File.GetLastWriteTime(pathToYourConfigFile);
    

    If the new write time was smaller than the recorded one, the config file would get reloaded. I used a small XML file for configuration. You can access this file on disk or with HTTP (http://somedomain.com/config.xml). In my case I used HTTP because the config file was not on the same server as the service.

  • Presumably you mean that you want to change the periodicity of your timer, i.e., how often it expires.

    I'd suggest keeping the timer period in the configuration file. Use a FileSystemWatcher to keep track of this file -- it will fire an event when the file changes. When the watcher detects that the file has changed and your code determines that the period is different, update the expiration period for the timer. Depending on whether you have the timer set up to automatically reset or you "manually" reset the timer on expiration, you may need to handle it differently. If it's automatically reset, then you'll need to stop and restart it when the value changes. If it's "manually" reset, then it could be set up to just pick up the new value on timer expiration.

    An example of how to use the FileSystemWatcher and interacting with the ConfigurationManager class (to read the configuration) can be found at MSDN.

0 comments:

Post a Comment