Creating a windows service in .net turns out to be a really simple thing. Instead of me doing a write up on it i will give you a like to a walkthrough on msdn and you can check it out.
http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.80).aspx
But what i will say is that i wanted to be able to edit the service configuration once it was started through a desktop interface. This was accomplished by saving the data to the registry and letting the service get its configuration from there. My Service itself runs a timer and checks periodically for new settings from the registry. I played around with a thread in a wait state but this took up too much cpu. A timer works very well with very consistent results.
The desktop app that was created to modify the settings was a simple .net app and it basically just formats data and modifies the appropriate registry values. here is a small snippet of getting and adding data to the registry.
Getting Data
Dim regKey As RegistryKey regKey = Registry.LocalMachine.OpenSubKey("Software\PPSPlus\MigrationApp\Data", False) If regKey Is Nothing Then Registry.LocalMachine.CreateSubKey("Software\PPSPlus\MigrationApp\Data") regKey = Registry.LocalMachine.OpenSubKey("Software\PPSPlus\MigrationApp\Data", False) End If Dim lr As String = regKey.GetValue("LastRun", "") Dim db As String = regKey.GetValue("Debug", "") Dim pk As String = regKey.GetValue("PPSImportKey", "") RegImportData = regKey.GetValue("ImportData", "") RegImportType = regKey.GetValue("ImportType", "") Dim df As String = regKey.GetValue("ForceRun", "")
Adding Data .
Dim regKey As RegistryKey regKey = Registry.LocalMachine.OpenSubKey("Software\PPSPlus\MigrationApp\Data", True) regKey.SetValue("LastRun", DateTimePicker1.Value.ToString) regKey.SetValue("Debug", ComboBox1.SelectedItem.ToString) regKey.SetValue("PPSImportKey", TextBox3.Text) regKey.SetValue("ImportData", TextBox4.Text) regKey.SetValue("ImportType", ComboBox2.SelectedItem.ToString) regKey.SetValue("ForceRun", ComboBox3.Text)
And that’s all he wrote.
4 Responses to Creating Windows Services