C# Windows 服务开发并启动
来源:互联网 发布:c4d r18 mac版下载 编辑:程序博客网 时间:2024/05/21 06:56
1、新建项目选择 Windows-----> Windows Service 即创建 windows service 项目。
2、在 ProjectInstaller.cs 设计模式下,点右键,选择 "Add Installer" ,点击 ServiceInstaller1 属性中修改 ServiceName 为自己的 service name, StartType 修改为 AutoMatic 即 自动启动,如手动启动则修改为 Manual。
3、在 ProjectInstaller.cs 设计模式下, 点击 ServiceProcessInstaller1, 在属性中修改 Account 修改为 LocalSystem。
4、在 Service1.cs 中, OnStart(string[] args) 方法即开始定义自己 windows service 要处理的业务。我觉得一般处理方式是放一个 timer于该Form 中,
(注意:该 timer 为 System.Timers 命名空间下的 Timer, 而非默认的 System.Windows.Forms 命名空间下的 Timer)
然后通过 timer 循环处理事件处理自己的业务。
5、部署 windows service ,可以使用 visual studio 自带工具 InstallUtil.exe, installutil yourproject.exe (此处 yourproject.exe 为自己开发的服务名编译后的exe 文件),如果要卸载,则加参数 -u.
6、一般开发后虽然服务设置为 Automatic ,但首次部署后必须要手动启动下才可,搜索了一下,发现博客园中有人如下解决。
该解决原址:http://www.cnblogs.com/wfwup/archive/2009/01/14/1375382.html
在ProjectInstaller.cs 设计模式下,事件 AfterInstall 事件中加入如下代码便可启动:
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string cmdStr = string.Format("sc start {0}", this.serviceInstaller1.ServiceName);
p.StandardInput.WriteLine(cmdStr);
p.StandardInput.WriteLine("exit");
- C# Windows 服务开发并启动
- C#编写Windows服务并安装与启动
- C# 开发windows 服务
- C#开发windows服务
- c#开发windows服务
- 遍历windows服务并启动指定服务
- C#启动停止windows服务
- C#启动windows服务浅析
- C#启动,停止Windows服务
- 通过C#启动windows服务
- C# Windows 服务安装并运行服务
- 创建windows服务,并自动启动
- windows下安装并启动redis服务
- 用C#开发Windows服务
- c#创建windows服务步骤并打包
- C#创建windows服务并定时执行
- C#创建windows服务并定时执行
- c# 通过Windows服务启动外部程序
- 4.4 编译
- setjmp和longjmp
- 开源ESB项目中的领头羊
- “general error c101008d”错误的处理
- [转]C#动态生成文字图片
- C# Windows 服务开发并启动
- Google Maps API Key申请记
- DWRUtil.addRows
- ESB项目需求分析和方案设计浅谈
- Mac 显示隐藏文件的方法
- 有订单表SO_Table,单号字段RefNo VARCHAR(10),需要实现自动编号,格式为YYYYMMXXXX,其中XXXX为序号,如:2004050001,2004050002……2004059999等,采用Transact-SQL实现新订单编号的思
- Java时间类以及函数(网络转载)
- meego obs 搭建全记录
- 过去的2010年