Skip to main content

How to Create Window Service and execute it

Dear All
If you are looking for develop you own window service than below is the simplest method to create it

The original reference for this post is following
http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/


Here we go...

This article explains a step-by-step process of developing and installing a Windows Service to do a scheduled job based on a time interval.

Open Visual Studio and from the menus select "File" -> "New" -> "Project...".

A New Project window will open. Choose "Visual C#" >> "Windows" project type and select "Windows Service" from the right hand side and name the project "TestWindowsService" as shown in the following screenshot.







After you click "OK", the project will be created and you will see the design view of the service as shown in the following screen. Right-click the "Service1.cs" file in Solution Explorer and rename it "to" Scheduler or any other name that you want to give it. Then click “click here to switch to code view”.



In the code view, you can see two methods called OnStart() and OnStop(). The OnStart() triggers when the Windows Service starts and the OnStop() triggers when the service stops.


Right-click the TestWindowService project, add a new class and name it "Library.cs". This class will be useful to create the methods that we require in the project. If your TestWindowService is a big project, you can create a ClassLibrary project and reference it to your TestWindowService.



Library.cs

Make the class public and declare it as a Static class.



Create a log method (WriteErrorLog) to log the exceptions.



Create one more log method (WriteErrorLog) to log the custom messages.



Scheduler.cs

Now return to our Scheduler.cs file and declare a Timer.



Write the following code in the OnStart() method and timer1_Tick():



Write the following code in the OnStop() method:


Scheduler.cs [Design]

Now return to the Scheduler.cs [Design] and right-click on the editor window then click "Add Installer".


Then you can see that there will be a new file called "ProjectInstaller.cs" as shown in the following.





Right-click on the "serviceInstaller1" and click "Properties".



Change the ServiceName to "Test Windows Service" (or your own name) and StartType to "Manual" (or you can choose "Automatic" if you need this service to be automatic).



Right-click the serviceProcessInstaller1, go to the properties window and change "Account" to "LocalSystem".



Build the project to see the .exe file at the location where you created the solution.



That's all. Your Windows Service is all ready to install in your machine.

Installing the Windows Service

Go to "Start" >> "All Programs" >> "Microsoft Visual Studio 2012" >> "Visual Studio Tools" then click "Developer Command Prompt for VS2012".

Type the following command:

InstallUtil.exe  <physical location of your TestWindowService.exe file>

in my case it is :

InstallUtil.exe  "C:\Sandbox\WindowServices\TestWindowService\TestWindowService\bin\Debug\TestWindowService.exe"

and press Enter.

Here you go, the TestWindowService is installed successfully.



How to start the Windows Service

Since we chose StartType = Manual, we must start the Windows Service manually by visiting the "Services and Applications" window in the computer.



Select the Test Windows Service and click "Start" to start the service. Go to the "TestWindowService.exe" location to see the logs.

LogFile.txt

Since we are tracking our Windows Service by writing some logs to a .txt file called LogFile.txt, we can test the working condition of our Windows Service by looking at this log file.



As you can see in the preceding screen, you can find the LogFile.txt file at the physical location that your TestWindowService solution exists.

Click the LogFile.txt to see the logs, whether our service is doing the job that we set it to do for every 30 seconds.



If you look at the preceding log file, we can prove that our Windows Service is running and doing the job that we wanted on a 30 seconds interval.

Stop the Windows Service

To stop the Windows Service, just click "Stop" link in the Services window by selecting our TestWindowService.

Logfile after stopping our service:




Type the following two commands in the "Developer Command Prompt for VS2012" to uninstall the TestWindowService.exe.
cd <physical location of your TestWindowService.exe file>
and press Enter. In my case it is:
cd C:\Sandbox\WindowServices\TestWindowService\TestWindowService\bin\Debug
InstallUtil.exe /u “TestWindowService.exe”
And press enter.
After executing the preceding commands, the TestWindowService will be uninstalled from your computer.

Summary

In this article, I explained how to develop a Windows Service and install it using InstallUtil.exe from a command prompt.

I believe that I have explained each step clearly that can be easily understandable for a beginner to develop and install a Windows Service.

If you want to register the application on your deployment enviroment where visual studio is not present then run the install.util command by following location 
C:\Windows\Microsoft.NET\Framework\v4.0.30319
Change the version as you have in my case i have v4.0.30319


I hope this article will help you 
Regards 
Rashid Imran Bilgrami

Comments

Popular posts from this blog

How to convert and crack windows server 2012 from Evaluation to Full

Dear All This is a way how you Convert Evalution to Full Step1: Open CMD and run following command DISM /online /Get-CurrentEdition <edition ID> is like ServerStandard with out Eval Step 2: DISM /online /Set-Edition:<edition ID> /ProductKey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX /AcceptEula WINDOWS SERVER 2012 Serial Key Windows Server 2012 DataCenter: 48HP8-DN98B-MYWDG-T2DCC-8W83P Datacenter: Y4TGP-NPTV9-HTC2H-7MGQ3-DV4TW Standard: XC9B7-NBPP2-83J2H-RHMBY-92BT4 Standard R2: DBGBW-NPF86-BJVTX-K3WKJ-MTB6V Server Essentials: K2XGM-NMBT3-2R6Q8-WF2FK-P36R2 For Standard R2 here is a command For R2 its like that DISM /online /Set-Edition:ServerStandard /ProductKey:DBGBW-NPF86-BJVTX-K3WKJ-MTB6V /AcceptEula Regards

SugarCRM Footer Logo Remove & SugerCRM violation Message Remove

Hi Reader, Let us discuss about how to remove the SugarCRM Footer logo.. You all are aware of the power of the SugarCRM Tool and also must be very eager to remove the footer in order to make it look more professional. So below mentioned are some tricks for removing the footer from SugarCRM Community edition. 1)  O pen-modules/Administration/ updater_utils.php Add-exit() ; in between   function check_now()   and   return . any where By doing this u can remove 'powered by sugar crm' footer logo. 2) Go to  modules/Trackers/Tracker.php, line 128, in the 'logPage' function. Drop the 'echo' statement. 3) Now to root(Sugarfolder)\include\mvc\view\sugarview.php and modify the line array(show header => true, show subpanel => true...........and so on) and make the changes to showfooter=>true to  showfooter=>false Now how you remove the violation  Go to include /utils/ mvc_utils.php and remove the line or code b...

DNN 7 Won't go into edit mode or not working with control bar

Dear All, Today when i install the DNN 7 then i found and issue in edit mode after installation. The problem is when i go to edit mode the page refresh with out enabling the edit mode, after a short research on Google i found the solution. I hppe it save your time 1. backup your web.config 2. Open your web.config in a text editor 3. within the web.config file, find the <system.webserver><modules> section. 4.  If the <modules> section says <modules runAllManagedModulesForAllRequests="false"> change it to <modules runAllManagedModulesForAllRequests="true"> OR  if it just says <modules>, change it to <modules runAllManagedModulesForAllRequests="true"> 5. Save the config file. 6. Retry by refreshing the page and trying again.  You can also test this out by trying to a do a journal post (which also uses the services framework in an authenticated way. If some how its will not work then, you must chang...