Building a Simple Window Service Application in Visual Studio 2010 That Runs, Sleeps and Stops

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”

 

image

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”.

image

Now, you’ll get to “gear” icons that you can set properties for.  They are “serviceProcessInstaller1” and “serviceInstaller1” as follows:

image

Select “serviceProcessInstaller1” and in it’s properties window change the account to “LocalSystem”.  You should have a screen that looks like the following:

image

Then, on the other gear “serviceInstaller”, change the nanes as follows.  I’m naming my service “MyDoNothingService”.

image

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

 

image

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.

image

(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:

image

In your event viewer, you should see you service starting and stopping as follows:

image

 

 

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

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP for the past 7 years. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Comments

  1. hi,
    something is missing, because I get :
    ….
    Remove InstallState file because there are no installers.

    The Commit phase completed successfully.

    The transacted install has completed.

    the new service isn’t listed in the list

Your Comments

*

Protected with IP Blacklist CloudIP Blacklist Cloud

Follow

Get every new post delivered to your Inbox

Join other followers: