Table of contents:
- Windows Service Introduction
- Windows Service Implementation in C#
- Windows Service Sample Project
Imagine that you as a programmer need an application that non-stop running to do some task done like capturing screen every hour, or providing tcp connection as a server, even backing up database automatically every months. This task could be solved easily with normal software like console application, windows form or wpf. But, it will pop out a something to the screen, and it is quite hard enough to implement pause on that application. Then, Windows service come to help. Windows service is focused on process rather than user interactions. Did you know? Almost database application using service to handle SQL request from SSMS/PHPMyAdmin/etc. the next topic is tutorial on starting windows service applications.
Open visual studio (I’m using VS2012), and then create new project.
Select windows service template project, see picture above.
Normally, you should see Service1.cs as Windows Service, you can rename it with whatever you want. And open its code, you should see Constructor, OnStart and OnStop method in this Class.
You could append OnContinue and OnPause method for doing something if the service was paused and define your service name. The method itself has intuitive method name, so you can do something in OnStart method, like applying scheduler, assign new object, write to a file or initialize an Timer class.
Whatever that you implements in that method, it will executed if service was installed on your machine, so the first step is to define an installer.
On Solution Explorer, right click the project and add new item.
Select installer class and add to your projects.
Right click your installer and then append code like image below.
And don’t forget to add using System.ServiceProcess;
And that’s it, if you try to running your application you should see pop-up warn you that service failure to run.
We require next step to deploy this application. The next step is could be achieved in many ways, sometimes it’s very complicated. But in this tutorial we will installing this service with “--install” parameter and “--uninstall" parameter to uninstall the service.
Open the project property.
And then set project as a console application, you’ll notice that we will deploy this as console application, but why? Maybe it’s a question in your head, so keep reading to find the answer.
The reason that we implement this service as a console application is to enable us to install on command line ways such as database like mongod.exe.
Below is sample installation using cmd.
And then in our case, we just applying --install and --uninstall parameters.
And then open your program.cs in your windows service project and add code like image below.
Don’t forget to add using System.Configuration.Install; and using System.Reflection;
If we see through the code you notice that System.Environment.UserInteractive is a determiner wheter we run this application as a service or not. And when we use a command prompt to run application, UserInteractive must be true Boolean value. The application should be finish, the next step is to test application running or not. Build your project using f6 or ctrl+shift+b keys.and in bin folder inside you application directory you’ll find [your application name].exe. open cmd as an administrator and then go to directory where you find your executable files.
To see if your application is running, click start->run and type “services.msc”.
In image above, you can start, pause, continue and stop the services by right click on it.
Note that windows service is running under Session 0 with no desktop interaction which a normal application executed under another session. This is important because we cannot directly access to the current active desktop because windows service should still run even there is no user logon at that machine. But still we can use WINAPI to do dirty work such as create a new process as a logged on user. We can use sample Native code from http://stackoverflow.com/questions/9095909/createprocessasuser-creating-window-in-active-session and create process to run console application that take a screenshot from the desktop.
Please download attached project to see full source code of how we could take a screenshot from windows service every 5 secs.