Windows Services

.net平台下开发Windows 服务

1.创建windows service 参考网址

http://www.cnblogs.com/tuyile006/archive/2006/11/27/573654.html

http://www.cnblogs.com/tyb1222/archive/2010/09/06/1819020.html

2.安装及卸载windows service

 安装本地服务:Run ->cmd 命令行执行

 C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe C:\MyService\MyService\bin\Release\MyService.exe 

卸载本地服务,

可以通过命令行输入如下命令 sc delete ServiceName

3.调试服务

参考http://www.cnblogs.com/xiaoxiangfeizi/archive/2012/04/18/2454715.html

http://msdn.microsoft.com/zh-cn/library/7a50syb3(v=vs.80).aspx

调试服务代码步骤:

1,获取并安装Debug模式下的服务。C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe C:\MyService\MyService\bin\Debug\MyService.exe

2.在OnStart方法上设置断点(将Interval值设置大点,太小可能服务已经开始,服务的进程还未添加)

3.选择Debug中的Attach to Process

可通过windows 任务管理器查看服务的进程,进程名就是服务名。如果process当中没有服务的进程名。勾选Show processes from all users

选择你的服务进程名点击Attach 即可调试代码。

 protected override void OnStart(string[] args)
        {
            if (this.timer1 == null)
            {
                this.timer1 = new System.Timers.Timer();
            }

            this.timer1.Interval = 5000;
            this.timer1.Enabled = true;
            this.timer1.Start();
            this.timer1.Elapsed += this.Start;
        }

 

 private void Start(object sender, System.EventArgs e)
        {
            this.timer1.Stop();
            MessageBox.Show("服务开始运行了");

            this.timer1.Start();
        }

Note:timer1.stop 是指停止 该事件。因为是单个线程,为防止死循环所以开始事件之前先将timer停掉,防止事件循环,执行完工作后,在将timer开始。

posted @ 2012-03-30 17:23  多远才是未来  阅读(202)  评论(0编辑  收藏  举报