A windows service is nothing but a program. It can also be called as a background program. Microsoft provides “System.ServiceProcess” assembly for the creation of windows services.
A windows service is nothing but a program. It can also be called as a background program. Microsoft is providing “System.ServiceProcess” assembly for the creation of windows services.
Creation of Windows Service:
Here I am going to create a windows service through which you can start and stop a particular service. Now I am going to create a windows service for a Solitaire game through which you can start and stop the application according to your requirement.
Step-1: Design the logic for the windows service.
Open visual studio >> Click file menu >> Select new project >> Expand visual c# >> Then select windows >> Then windows service template >> Give the name as “SolitaireServiceApp” >> click ok.
Now it will open Service1.cs design window as shown in below image.
Click project menu >> Select add reference >> Then click .NET tab >> Select “System.Windows.Forms” assembly >> click ok as shown in below image.
Now you can see that it will automatically add the references as shown in below image.
Open the tool box >> Right click on the general tag >> Then select choose items >> From the items choose timer of having namespace “System.Timers” >> then click ok. Below is the image.
After clicking ok, you will get a screen of below shown image.
• Then double click on the timer control placed within general tab.
• In timer control properties set -> Enabled=false. Then double click on the timer control.
• Import the namespace “Using System.Windows.Forms” and write the below code within timer1_Elapsed event procedure
publicpartialclassService1 : ServiceBase
protectedoverridevoid OnStart(string args)
timer1.Enabled = true;
timer1.Enabled = false;
privatevoid timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
Process x = Process.GetProcesses();
for (int i = 0; i < x.Length; i++)
if (x[i].ProcessName.ToUpper() == “SOL”)
MessageBox.Show(“You are not authorized to use this service.”);
Step-2: Adding the installers.
• Go to Service1.cs design mode
• Right click inside the design window.
• Then select add installer.
Below is the sample image.
Now two installers will be added that is “serviceProcessInstaller1” and “serviceInstaller”1 as shown in below image.
Go to serviceProcessInstaller1 properties -> set Account = Local System
Go to serviceInstaller1 properties -> set Service Name = sol service that is for solitaire game.
This name will appear within the Services.msc window after installation.
Step-3: Converting into an exe file.
• Click built menu
• Then select build SolitaireServiceApp.
Now it will generate an .exe file with in the location where you have created your project. That is “F:\csharpnet\SolitaireServiceApp\SolitaireServiceApp\bin\Debug” location.
Step-4: Install the above created service within Services.msc
• Click start menu
• Select programs
• Select Microsoft visual studio 2010
• Then select visual studio 2010 command prompt
• Go to your drive where you have saved your application i.e “F:” and give the command as shown below:
Then press enter key and you will expect to get the below image.
Now open your “Services.msc” window and observe a service will be viewed with a name “Sol service” as shown in below image.
Right click on the Sol Service, and then select properties as shown below.
Then select logon tab.
Then check the option “allow service to interact with desktop” as shown below image.
Then click apply, click ok.
The above option is required only if message box class is used within the windows service.
Now right click on the “Sol service” and select start as shown in below image.
By default the startup type of every service will be manual. If startup type is manual, then the service must be started explicitly by the user.
But if you want to make it automatic as soon as your system will start, then
• Right click on the “Sol Service”
• Then select properties.
• Then select startup type=Automatic
• Then click apply, then ok. Below is the image.
Now it’s the time to enjoy our created service. As we have already started the service.
Now try to open the solitaire game.
Goto start menu
Then select run and type there “sol”
When you will press enter key, at the same time you will get an error message as shown below image.
In this way you can protect your services in your system by creating windows services. So to resume your “Sol” game service again go to that “services.msc” window and stop the service. After that you can able to use the solitaire game.
To uninstall the above created service, give the command as shown in below image
With the help of this you can uninstall the service. In the above image, after debug write”\SolitaireServiceApp.exe”. After that press enter key.
By using this you can start and stop any application that is running in your windows.