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服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作不成功,可能是开发环境或项目配置的不同,这里 ...
随机推荐
- HIVE中内连接和左半连接不一致问题
一.理论 HIVE中都是按等值连接来统计的,理论上两种写法统计结果应该是一致的: 二.实际情况 但实际使用中发现两种写法会返回的结果,总会有一些差距虽然差别不大,但让人很是困惑. 三.原因 当使用jo ...
- iOS 7隐藏statusbar
- (BOOL)prefersStatusBarHidden { return YES; } [[UIApplication sharedApplication] setStatusBarHidden ...
- 史上最全github使用方法:github入门到精通--备用
[初识Github] 首先让我们大家一起喊一句“Hello Github”.YEAH!就是这样. Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理 ...
- 微软Hololens学院教程- Holograms 101: Introduction with Device【微软教程已经更新,本文是老版本】
这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...
- Elasticsearch基础概念理解
熟悉ES中的几个关键概念: 节点(Node):一个elasticsearch运行的实例,其实就是一个java进程.一般情况下,一台机器运行在一台机器上. 集群(Cluster): 好几个有相同集群名称 ...
- 安全测试常见的10个问题 ZT
1, 问题:没有被验证的输入 测试方法: 数据类型(字符串,整型,实数,等) 允许的字符集 最小和最大的长度 是否允许空输入 参数是否是必须的 重复是否允 ...
- List中toArray()的使用方法
当我们需要把一个链表中的元素放入数组时,jdk给我们提供了一种方法,也即运用toArray(),方法的使用如下: public class Test { public static void main ...
- Memcached总结二:Memcached环境安装设置以及连接memcache服务器
1 在Ubuntu上安装Memcached 要在Ubuntu上安装Memcached,打开终端,然后输入以下命令: $sudo apt-get update $sudo apt-get install ...
- 2、RenderScript的计算(2013.05.07)
一.官方文档翻译: Android Renderscript计算 参考网址: http://blog.csdn.net/fireofstar/article/details/7748143 http: ...
- MVC——数据库增删改查(Razor)——Html语法
一.显示界面 .Models(模板) private MyDBDataContext _context = new MyDBDataContext(); public List<Info> ...