[转载]C# 写Windows服务程序
来源:互联网 发布:linux常用网络命令大全 编辑:程序博客网 时间:2024/05/19 06:49
*本文非博主原创,收藏时未保存原作者文章链接,见谅,版权归原作者所有。本文只稍作修改。
1、新建C# Windows服务:windows service工程
2、新建windows service工程后,系统自动生成一个Service1.cs文件,默认是其设计视图。选择查看其代码,默认有构造函数、OnStart、OnStop三个函数
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;using System.Threading.Tasks;namespace WindowsService1{ public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { } protected override void OnStop() { } }}
3、新建了C# Windows服务之后,还要设置该服务运行的周期,左侧的ToolBox中有两个timmer,一个在组件下,一个在windows form下,可惜这两个都不能用,我们要手工新建一个timmer,并设置其属性和事件。
public WindowsServiceDemo() { InitializeComponent(); System.Timers.Timer t = new System.Timers.Timer(1000);//实例化Timer类,设置间隔时间为10000毫秒; t.Elapsed += new System.Timers.ElapsedEventHandler(TimeElapse);//到达时间的时候执行事件; t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; } publicvoid TimeElapse(object source, System.Timers.ElapsedEventArgs e) { FileStream fs = new FileStream(@"d:\timetick.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("过了一秒 " + DateTime.Now.ToString() + "\n"); m_streamWriter.Flush(); m_streamWriter.Close(); fs.Close(); }
4、服务编写之后,还不能由SCM(服务控制管理器)进行管理,需要给该服务添加装载器。在Service1.cs的设计视图,点击右键,选择“添加装载器”,系统默认就会添加ProjectInstaller.cs这个类。
5、添加该类后,在该类的设计视图上可看到serviceInstaller1和serviceProcessInstaller1,分别设置其属性。
设置serviceInstaller1的运行方式为手动或者自动
设置serviceInstaller1的ServiceName,设置为什么,服务列表中就显示什么
设置serviceProcessInstaller1的运行账号为LocalSystem
6、编译该工程
7、使用vs自带的命令行工具,运行installutil 编译生成的exe
8、在系统的服务中可看到我们创建的服务。
需要注意的是:
如果你修改了这个服务,路径没有变化的话是不需要重新注册服务的,如果路径发生了变化,需要先卸载这个服务InstallUtil.exe /u参数,然后再重新安装这个服务,不能直接安装。还有就是C# Windows服务是没有界面的,不要企图用控制的输出方式来输出一些信息,你只能添加一个EventLog,通过WriteEntry()来写日志。
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- [转载]C# 写Windows服务程序
- c#写windows服务程序
- c#写windows服务程序
- c#写windows服务程序
- c#写windows服务程序
- C#写Windows Service(windows服务程序)
- 关于C# 写Windows服务程序
- C# 写windows服务
- C#写windows服务
- c#写windows 服务
- C#写Windows服务
- c#写windows服务
- c#写windows服务
- C#写windows服务
- 用Visual C#创建Windows服务程序(原文转载)
- 用C#写一个简单的WINDOWS服务程序
- 用C#写一个简单的WINDOWS服务程序
- 用C#写一个简单的WINDOWS服务程序
- BeginInvoke与EndInvoke方法解决多线程接收委托返回值问题
- HDU 寒冰王座
- 图片上传并预览(IE,火狐浏览器兼容问题)
- Request读取中文数据乱码的原因,以及解决办法
- 备忘:Api查询网址
- [转载]C# 写Windows服务程序
- 解决全屏UI中输入法的问题(一)
- POJ 1703 Find them, Catch them
- Android开发_viewpager作为header加入到listview
- 一套非常适用权限系统快速开发框架源代码
- Life/Asia table struct copybook convert to java pojo automaticlly
- 经典回顾超炫酷阅读效果3D Book使用方法
- Algorithmic Implementation series(8) Implementation of Counting_Sort
- 均值、中值滤波、孤点检测