Ok, on to the Windows service.
There are a couple of things about a Windows service. First, you need a timer and second, unless you want to wait until the timer ticks down to 0, you need to place your code in a routine and call it for the first time from the OnStart() method.
So, in our OnStart() method, we want to first execute the work to be done and then we want to initiate and start our timer for the next iteration.
Here is the code we will use
public static string sService = "SubscriptionSvcMonitor";
public static string sRegKeyPath = @"Software\Boeing Configuration\SubscriptionSvcMonitor";
protected override void OnStart(string[] args)
{
DoWork();
//Connect to HKEY_LOCAL_MACHINE
RegistryKey regTimer = Registry.LocalMachine;
try
{
//Define the time interval as double and get the value from the registry.
//Value must be a double but the registry string is a string variable so
//we need to explicitly convert it to a double. (Convert.ToDouble())
double lInterval = Convert.ToDouble(regTimer.OpenSubKey(sRegKeyPath, false).GetValue("Interval").ToString());
//Multiply lInterval by 1000. (1000 ticks equals 1 second)
Timer aTimer = new Timer(lInterval * 1000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
catch (Exception ex)
{
//If we have a problem with the registry, write a critical alert to the application log
//and set the time interval to 1 hour.
EventLog.WriteEntry(
sService, ex.Source + Environment.NewLine + ex.Message, EventLogEntryType.Error, 234);
Timer aTimer = new Timer(3600000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
finally
{
//Close the registry
regTimer.Close();
}
}
DoWork() is the method where I'm storing the code to be executed when the timer hits 0 and on the initial start of the service so we will cover that in another posting.
So what are we doing here? Well, we are opening a registry key and getting the interval for our timer. The timer requires a Double so we need to convert our string value from the registry to a double and assign it to a variable. I did this just to make it more clean, you could probably just add all the code for the registry value to the timer directly but it makes for more difficult reading.
The timer runs on ticks. One thousand ticks is equivalent to one second so the registry value is the number of seconds we want and then we multiply that by 1000.
Next, we need to call the OnTimedEvent method every time the Timer.Elapsed() method is fired off and we reset our timer and start again.
We threw it all into a try block because if you don't have a registry key to get the interval from, you would get an error for object variable not set to an instance of an object so we catch that and set a default value of 3600 seconds or 60 minutes and start the process.
To debug a Windows service, you cannot just hit F11 and step through the code. You need to install the service using the install utility with the .NET SDK and then start it. Once started, go back to Visual Studio and select debug -> Attach to Process and attach to your service thread.
When the attach to process form opens, make sure you look for processes from all users and not just your own. You will see your service twice, one under your user ID and one under the ID of the identity of your newly installed service. The only one you can attach to is the one installed and not the one under your user ID.
At this point, you should be able to create an empty Windows service.