Windows 服务 创建 和 安装 -摘自网络
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

InstallUtil.exe “TestWindowService.exe”
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.
Windows 服务 创建 和 安装 -摘自网络的更多相关文章
- Windows服务创建及安装
Windows服务创建及安装http://www.cnblogs.com/tuyile006/archive/2006/11/27/573654.html
- .net windows 服务创建、安装、卸载和调试
原文:http://blog.csdn.net/angle860123/article/details/17375895 windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境 ...
- 基于C#&.net2.0的windows服务创建与安装
起因:一台服务器中部署的程序,停电后未按照计划任务正常启动. 一.创建并配置Windows服务程序 开发工具VS2015 Framework版本2.0 1.新建Windows服务 2.在Service ...
- 关于windows服务的编写/安装/与调试
前注: 首先,这篇文章是从网上转过来的,因为最近有个项目,需要编写一个Windows Service来定时执行程序,网上很容易找到了这篇文章,大概看了一下,文章讲的还是很详细的.不过这篇文章应该是.n ...
- C#-Windows服务创建和运行
Windows服务创建和运行 适用场景: ASP.Net通常是一个无状态的提供程序,不支持持续运行代码或者定时执行某段代码,所以我们需要构建自己的Windows服务来运行那些定时任务. 项目中需 ...
- C# Windows服务创建安装卸载
一.创建Windows服务 使用VS创建一个新的windows服务应用程序 创建完成之后 二.相关配置 修改Service1名称为StartService(可以不改,自行选择) 添加安装程序并修改配置 ...
- windows服务搭建(VS2019创建Windows服务不显示安装组件)
1.创建windows服务应用 2.右键查看代码 3.写个计时器Timer using System.Timers; 如上图,按tab键快速操作 会自动创建一个委托 改为下边的方式,打印日志来记录 ...
- windows服务创建与管理
安装windows 服务 C:\Users\chensimin>cd \ C:\>cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 C:\W ...
- C# windows 服务编写及安装
最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作不成功,可能是开发环境或项目配置的不同,这里 ...
随机推荐
- 【BZOJ】1070: [SCOI2007]修车
1070: [SCOI2007]修车 Description 同 一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需 ...
- import information website
1. 一个很好的展示如何review paper和response to reviewer的网站:openview 该网站给出了许多paper review的例子(如何你是reviewer,那么可以参 ...
- delphi xe5 android 关于文件大小的几个问答O(∩_∩)O~
摘自:http://blogs.embarcadero.com/vsevolodleonov/2013/09/19/are-you-asking-about-app-size-by-delphi-fo ...
- c++ 输入一行字符串
]; //cin>>str1; 方式1 不能统计(录入)空格后的字符 cin.); //方式2 能统计空格后输入的字符 按回车键输入结束 get()会将换行符保存在序列里 //gets(s ...
- MySQL注入中load_file()函数的应用
常用的一些Load_File()函数攻击手法:
- unity3d 下操作excel 与打印
原地址:http://www.colabug.com/thread-1146390-1-1.html 因项目需要,需在unity下实现写入excel.打印功能.因之前在winform里操作过excel ...
- windows 安装mysql的时候最后执行一直停留在Write configuration file
出现原因:MySQL安装路径出现中文,特殊字符.或是重新安装MySQL后经常遇到.前者是路径不允许出现中文名称,后者是由于卸载不干净. 我就是因为重新安装了MySQL,卸载不干净,才会导致之后这个错误 ...
- easyui源码翻译1.32--ComboGrid(数据表格下拉框)
前言 扩展自$.fn.combo.defaults和$.fn.datagrid.defaults.使用$.fn.combogrid.defaults重写默认值对象.下载该插件翻译源码 数据表格下拉框结 ...
- 【Xamarin挖墙脚系列:IOS-关于手机支持的屏幕方向】
原文:[Xamarin挖墙脚系列:IOS-关于手机支持的屏幕方向] 设置支持的屏幕方向有两个级别,一个是app级别的,另一个是viewController级别的. app 级别的可以在[target] ...
- Python标准库之urllib,urllib2
urllib模块提供了一些高级接口,用于编写需要与HTTP服务器交互的客户端.典型的应用程序包括从网页抓取数据.自动化.代理.网页爬虫等. 在Python 2中,urllib功能分散在几个不同的库模块 ...