What a Windows Service is

  • Enables you to create long-running executable applications that run in their own windows session.
  • Can be automatically started when the computer boots, can be paused and restarted without any user interaction.
  • Easily installable by running the command line utility InstallUtil.exe and passing the path to the service's executable file.

Why a Windows Service?

One of the most common requirements of some businesses is long-running scheduled jobs based on some time interval. For example: sending a newsletter everyday afternoon or send an email alert to the customer for every one hour.

So building a Windows Service could be one of the reliable solutions to do the goal that can do the required job in the behind the scenes without interfering others users on the same computer.

Introduction

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:

cd <physical location of your TestWindowService.exe file>

in my case it is :

cd C:\Sandbox\WindowServices\TestWindowService\TestWindowService\bin\Debug

 
 
 
Next type the following command:

InstallUtil.exe “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.

  1. cd <physical location of your TestWindowService.exe file>
    and press Enter. In my case it is:
    cd C:\Sandbox\WindowServices\TestWindowService\TestWindowService\bin\Debug
  2. 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.

Windows 服务 创建 和 安装 -摘自网络的更多相关文章

  1. Windows服务创建及安装

    Windows服务创建及安装http://www.cnblogs.com/tuyile006/archive/2006/11/27/573654.html

  2. .net windows 服务创建、安装、卸载和调试

    原文:http://blog.csdn.net/angle860123/article/details/17375895 windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境 ...

  3. 基于C#&.net2.0的windows服务创建与安装

    起因:一台服务器中部署的程序,停电后未按照计划任务正常启动. 一.创建并配置Windows服务程序 开发工具VS2015 Framework版本2.0 1.新建Windows服务 2.在Service ...

  4. 关于windows服务的编写/安装/与调试

    前注: 首先,这篇文章是从网上转过来的,因为最近有个项目,需要编写一个Windows Service来定时执行程序,网上很容易找到了这篇文章,大概看了一下,文章讲的还是很详细的.不过这篇文章应该是.n ...

  5. C#-Windows服务创建和运行

    Windows服务创建和运行    适用场景: ASP.Net通常是一个无状态的提供程序,不支持持续运行代码或者定时执行某段代码,所以我们需要构建自己的Windows服务来运行那些定时任务. 项目中需 ...

  6. C# Windows服务创建安装卸载

    一.创建Windows服务 使用VS创建一个新的windows服务应用程序 创建完成之后 二.相关配置 修改Service1名称为StartService(可以不改,自行选择) 添加安装程序并修改配置 ...

  7. windows服务搭建(VS2019创建Windows服务不显示安装组件)

    1.创建windows服务应用 2.右键查看代码 3.写个计时器Timer  using System.Timers; 如上图,按tab键快速操作  会自动创建一个委托 改为下边的方式,打印日志来记录 ...

  8. windows服务创建与管理

    安装windows 服务 C:\Users\chensimin>cd \ C:\>cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 C:\W ...

  9. C# windows 服务编写及安装

      最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作不成功,可能是开发环境或项目配置的不同,这里 ...

随机推荐

  1. jcom2在win7 X86上操作Excel

    浅谈Java中利用JCOM实现仿Excel编程   在JAVA中使用JCOM和JXL注意要点: (1)在你的lib下要有jdom-1.0.jar,jxl-2.5.5.jar,jcom-2.2.4.ja ...

  2. uCGUI窗口的创建过程分析

    一.相关结构体和变量 窗口管理结构体 /* 窗口管理结构体 共30个字节 */ struct WM_Obj { GUI_RECT Rect; //窗口尺寸(x0,y0,x1,y1) 8个字节 GUI_ ...

  3. 初识Tower Defense Toolkit

    Tower Defense Toolkit 做塔防游戏的插件 主要层次如下图: 1GameControl _ _Game Control(Script) _ _ _Spawn Manager _ _ ...

  4. 上传项目到Github

    1.使用根工具(均是图形化的界面) TortoiseGit-1.8.12.0-32bit GitExtensions-2.48.05-SetupComplete 2.大致步骤 首先,你需要一个Gith ...

  5. Portal技术综述

    从上世纪八十年代起,随着个人计算机(PC)的发展,企事业单位逐步建立起计算机应用系统来处理传统事务.从简单的单机应用逐步发展到局域网系统,进而建立跨地区垮国家的广域网系统.从单纯的以科学计算和数据管理 ...

  6. [转载]MongoDB C# 驱动教程

    本教程基于C#驱动 v1.6.x . Api 文档见此处: http://api.mongodb.org/csharp/current/. 简介 本教程介绍由10gen支持的,用于MongoDB的C# ...

  7. WEB黑客工具箱之LiveHttpHeaders介绍

    一.LiveHttpHeaders之安装 自行百度 二.LiveHttpHeaders主窗口 根据我们目的的不同,LiveHttpHeaders有两种启动方法:当我们只想监视通信量的时候,可以从浏览器 ...

  8. 解决win8.1右键菜单出现在左边

    这个问题估计很少有人遇到,当在桌面上单击鼠标右键时,如果正常情况下,应该是在鼠标光标的右侧弹出来,除非右边的空间不够了,才在左侧弹出.但遇到故障,就是不论在桌面的哪里点右键,菜单都在左侧弹出,虽然不影 ...

  9. ArcGIS Engine Style文件操作

    对于一个GISer来说,地图,符号这些都应该有着比别人更深刻的理解和认识,作为平台软件都会提供一套自己的符号库,符号库里面根据类别和种类进行区分,因为点,线,面的自然存在和固有属性是不肯能让你用面状符 ...

  10. 应付发票审批 Hold and Release Names

    (N) AP > Setup > Invoice > Hold and Release Names 这里一般默认就可以了.Use the Invoice Hold and Relea ...