C#实现Windows服务
资源:Walkthrough: Creating a Windows Service Application in the Component Designer: https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx
注意事项
- 调试服务:先启动服务,然后Attach到服务进程。
- 需要添加安装模块,这样可以安装Windows服务。使用InstallUtil.exe安装。
- 服务运行在different window station,不是interactive的,如果服务弹出对话框,用户是看不见的,这时候可能中断服务,使服务失去响应。
安装服务。
- 命令。InstallUtil /i MyService.exe安装服务。InstallUtil /u MyService.exe卸载服务。
- 编程。ManagedInstallerClass
if (args[] == "-i")
{
ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
}
else if (args[] == "-u")
{
ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
}
控制服务状态:
- 命令。使用sc start ServiceName启动服务,sc stop ServiceName停止服务,sc query ServiceName查询服务状态。Sc delete ServiceName删除服务。
- 编程。System.ServiceProcess.ServiceController。 https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller(v=vs.110).aspx
- 服务管理界面。

创建服务。
如果创建新项目,可以使用Windows服务模板。

如果是现有项目,往项目中添加Windows服务模块。

在Main中调用ServiceBase.Run把服务跑起来。
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
在OnStart和OnStop中启动和清理工作线程。注意不要在OnStart中提供服务(比如while循环),否则服务一直会是正在启动状态,后面也就没有办法停止服务了。
protected override void OnStart(string[] args)
{
//启动后台线程,提供服务
}
在控制台测试。为了测试,我们可能在控制台跑服务。
private static void Main(string[] args)
{
if (Environment.UserInteractive)
{
var service = new Service1();
service.TestService(args);
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new Service1() };
ServiceBase.Run(ServicesToRun);
}
} ///Service1
public void TestService(string[] args)
{
this.OnStart(args); Console.ReadLine(); this.OnStop();
}
注意程序的编译输出类型应该为“控制台应用程序”。

添加安装模块。在Service的设计界面上右键,选择添加安装程序。

设置添加的Installer中的控件属性,包括服务运行的账号,服务名等。


设置服务状态
如果服务启动时间很长,可以在代码里面设置服务显示的状态。
public enum ServiceState
{
SERVICE_STOPPED = 0x00000001,
SERVICE_START_PENDING = 0x00000002,
SERVICE_STOP_PENDING = 0x00000003,
SERVICE_RUNNING = 0x00000004,
SERVICE_CONTINUE_PENDING = 0x00000005,
SERVICE_PAUSE_PENDING = 0x00000006,
SERVICE_PAUSED = 0x00000007,
} [StructLayout(LayoutKind.Sequential)]
public struct ServiceStatus
{
public long dwServiceType;
public ServiceState dwCurrentState;
public long dwControlsAccepted;
public long dwWin32ExitCode;
public long dwServiceSpecificExitCode;
public long dwCheckPoint;
public long dwWaitHint;
}; //在服务中导入方法
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool SetServiceStatus(IntPtr handle, ref ServiceStatus serviceStatus); //使用方法。
ServiceStatus status = new ServiceStatus();
status.dwCurrentState = ServiceState.SERVICE_START_PENDING; SetServiceStatus(this.ServiceHandle, ref status);
C#实现Windows服务的更多相关文章
- 基于SignalR实现B/S系统对windows服务运行状态的监测
通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windows服务肯定是必不可少的,我们还需要利用B/S与windows服务进 ...
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- 玩转Windows服务系列汇总
玩转Windows服务系列汇总 创建Windows服务 Debug.Release版本的注册和卸载及其原理 无COM接口Windows服务启动失败原因及解决方案 服务运行.停止流程浅析 Windows ...
- 玩转Windows服务系列——给Windows服务添加COM接口
当我们运行一个Windows服务的时候,一般情况下,我们会选择以非窗口或者非控制台的方式运行,这样,它就只是一个后台程序,没有界面供我们进行交互. 那么当我们想与Windows服务进行实时交互的时候, ...
- 玩转Windows服务系列——使用Boost.Application快速构建Windows服务
玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...
- 玩转Windows服务系列——Debug、Release版本的注册和卸载,及其原理
Windows服务Debug版本 注册 Services.exe -regserver 卸载 Services.exe -unregserver Windows服务Release版本 注册 Servi ...
- C# 开发windows服务的一些心得
最近在做一个windows服务的项目,发现并解决了一些问题,拿出来和大家分享一下,以下windows服务简称“服务” 文章会在适合时间更新,因为朋友们在不断提出新的意见或思路,感谢-.- 1.服务如何 ...
- 使用topshelf包装redis为windows服务
Redis服务端目前用的是控制台程序运行,部署的时候能作为windows服务后台运行感觉更好.找到一篇文章Running Redis as a Windows Service,利用win ...
- 编写Windows服务疑问1:操作过程
Windows 服务开发平时不太受人关注,毕竟那是高大上的项目类型,平常需求也用不上,很多老掉牙的家伙也只知有WinForm,仍不知有WPF,更别说Windows 服务了,正如陶渊明所写的,“不知有汉 ...
- C# windows服务制作(包括安装及卸载)
开篇语 因工作内容需要做一个windows服务,此前并没有相关经验,所以做了一个demo来跑跑这个梗(高手跳过,需要的来踩)- 效果如下:打开服务,可以找到我们新增的一个windows服务,这个dem ...
随机推荐
- (转)CSS中的绝对定位与相对定位定位
层级关系为: <div ——————————— position:relative; 不是最近的祖先定位元素,不是参照物<div—————————-没有设置为定位元素,不是参照物<d ...
- Javascript之Prototype
1.原型设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个类B,这个类是以A为原型的,并且能进行扩展.我们称B的原型为A. 2.javascr ...
- CSS元素定位6-10课
<精通CSS.DIV网页样式与布局>视频6-10课总结图: 元素定位 (1)float:left/right; 左浮动:脱离普通文档流向左浮动(即向左对齐):float属性必须应用在块级元 ...
- myeclipse10.7破解成功 但 无法打war包 提示:securecrt alert:integrity ch
myeclipse10.7破解成功 但 无法打war包 提示:securecrt alert:integrity check error 找了好久才找到解决办法 http://download. ...
- 修改安卓串口蓝牙app问题记录
* 在网上下载的安卓的蓝牙串口app都是基于eclipse的,但往as里边导入时都存在问题. 迫不得已最后我使用的办法还是在as下面新建工程,然后把相关文件导入.不过还是遇到了其他的问题. * 某个蓝 ...
- windows下Bullet 2.82编译安装(Bullet Physics开发环境配置)
平台:Win7,VS2010 1. Bullet库的组织 下图是Bullet_User_Manual中的截图: 从中可见,Bullet的LinearMath(线性数学模块),其上是BulletColl ...
- 水平ListView类
package com.hztbc.android.HorizontalListView; /* * HorizontalListView.java v1.5 * * * The MIT Licen ...
- DMA控制器
DMA控制器依赖于平台硬件,这里只对i386的8237 DMA控制器做简单的说明,它有两个控制器,8个通道,具体说明如下: 控制器1: 通道0-3,字节操作, 端口为 00-1F 控制器2: 通道 4 ...
- java数组引用
public class Arriy { public static void main(String args[]){ int data[]=new int[3]; data[0]=10; data ...
- HDU 1016Prime Ring Problem
http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入一个数,给出符合要求的素数环. 经典的dfs遍历. #include<iostream&g ...