Introduction
The goal is to create a simple Service in Windows 7 (or other similar OS’s) with Visual Studio 2010 that simply starts, sleeps for 15 seconds, then stops. I realize this is not that useful, but basically, it covers the case of building a service that actually does something and when finished stops. In my personal case, my thread has a while(true) loop which keeps looking for new work and only completes when certain conditions are met (like a fatal error that is non recoverable).
Microsoft has given us a pretty good set of docs and walk through. You can find them all here: http://msdn.microsoft.com/en-us/library/y817hyb6.aspx
The Steps
Create a new Visual Studio Project of type “Windows Service”
Once the project is created, make sure you have the design surface up for Service1 [Design] and right click on it and say “Add Installer”.
Now, you’ll get to “gear” icons that you can set properties for. They are “serviceProcessInstaller1” and “serviceInstaller1” as follows:
Select “serviceProcessInstaller1” and in it’s properties window change the account to “LocalSystem”. You should have a screen that looks like the following:
Then, on the other gear “serviceInstaller”, change the nanes as follows. I’m naming my service “MyDoNothingService”.
Now, rebuild your project and you’ll get an exe file in your /bin/Debug folder. Open that folder with the Visual Studio Command Prompt (elevated as admin). To install the service, enter the command:
InstallUtil WindowsServiceCreateSample.exe
You should get some messages that end with “The Commit phase completed successfully”, then “The Transaction install has completed”.
Now, you should see that you have the service in your services panel (you can get there by going to control panel / Administrative / Services or simple run “services” from your start button.
(sorry for the rename here to MyServiceTest, but I got distracted and had to come back and rename the project)
So, now we want to add something to this project. Let’s just add to the OnStart event of Service1.cs something that starts a thread that sleeps for 15 seconds, then stops the thread. Very simple code as follows:
protected override void OnStart(string[] args) { new Thread(() => { Thread.Sleep(15000); // sleep 15 seconds this.Stop(); }).Start(); }
Now, before we can install the service, we first need to uninstall it. Very easy, just add a /u to the end of your install command.
InstallUtil WindowsServiceCreateSample.exe /u
You should be greeted with “The uninstall has completed.”.
Now, when you try and reinstall, sadly, you might get this error: “Error 1001. The specified service has been marked for deletion”. If you do, thanks to a post by Mr. Laxdel, all you have to do is exit your services dialog and restart the service.
Assuming you left the default autoLog set to true as follows:
In your event viewer, you should see you service starting and stopping as follows:
Conclusions
That was pretty straight forward. Hopefully, you can follow along and generate the same thing yourself. Here is the source code attached for a visual studio 2010 project if for some reason you have trouble recreating it yourself. HTH’s!
Visual Studio 2010 Project Project