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

 

SV Code Camp is happening next weekend with well over 200 sessions.  The conference is really technology agnostic and try hard to keep it that way, but I am an ASP.NET guy these days so I think it reasonable I take my Code Camp Coordinator hat off for the moment and put on my ASP.NET hat.  As an ASP.NET Microsoft MVP, I feel I’m in a good position to give a tour of some sessions in ASP.NET and throw in my own 2 cents.  Below are a list of these sessions in no particular order.

 

Demystifying ASP.NET MVC, $(Lino).MVC{getknowledge()}

Sunday, October 9th 9:45AM

MVC (Model View Controller) is one of the hottest patterns around building web sites these days no matter what the technology. When Microsoft first introduced Web programming to Visual Studio, they did it such that almost anyone who had done windows programming (VB or other) could quickly come up to speed and build a web site. Personally, this what sucked me into web programming. Drag a button, drag a textbox, double click on the button and add some code to set the text box and WHAM, you have a web site. Unfortunately, as things get more complicated, this model does not scale so well. ASP.NET’s MVC implementation does scale, it takes a little bit of flipping your mind around if you are Web Forms programmer, but definitely worth learning about and seeing if it’s right for you.

Lino Tadros is one of my favorite presenters and I’m sure will give you a much better understanding of MVC and how it fits into the ASP.NET programming model. I strongly recommend this one. Lino is the CEO for one of the top Application development and training companies in the world that partners with Microsoft, Falafel Software. In other words, GO TO THIS SESSION!

 

Pragmatic JavaScript, jQuery & Ajax with ASP.NET

Saturday, October 9th 1:45PM

Damian Edwards is a Program Manager on the Microsoft ASP.NET team.  That’s right, he is flying here from Redmond just to present to us.  He’s also an awesome presenter and we are extremely lucky to have him at Code Camp!   Damien will tak about using JQuery while building sites with the Microsoft ASP.NET framework.  Something Damien knows very very well.  For those who don’t know, Damien is also leading the next generation efforts around ASP.NET WebForms which is the technology 90% of who use ASP.NET use (it’s what the silicon valley code camp web site is built with).

DON’T MISS THIS PRESENTATION!

 


WebMatrix In Depth

Saturday, October 9th  5:00PM

John Sheehan, one of the best guys I know at explaining complex things is presenting this session on WebMatrix.  I’m not sure everyone would agree with me, but for those people that really don’t like the heavy weight feeling of building web applications with Visual Studio, WebMatrix will make you feel right at home. It’s much lighter weight and also gives tons of punch.  Come see John and ask him a million questions!  He loves that.

 

All the sessions Tagged ASP.NET can be found here:  http://www.siliconvalley-codecamp.com/Sessions.aspx?sortby=title&by=category&tag=3


© 2012 PeterKellner.net. All Rights Reserved
Follow

Get every new post delivered to your Inbox

Join other followers: