中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > .NET Framework
用.net创建windowsservice的总结
作者:tojike 时间:2002-01-23 12:09 出处:互联网 责编:chinaitpower
              摘要:用.net创建windowsservice的总结

前言

net为创建windows service提供了专门的类库,结束了以前开发windows service窘迫的局面。你甚至可以不用添加一行代码,就可以用wizard生成一个windows service

一、wizard生成最基本的框架

此时,系统会为你生成一个框架,部分主要源代码如下:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.ServiceProcess;

namespace WindowsService1

{

       public class Service1 : System.ServiceProcess.ServiceBase

       {

             

              private System.ComponentModel.Container components = null;

             

        public Service1()

              {

                     InitializeComponent();

              }

              static void Main()

              {

                     System.ServiceProcess.ServiceBase[] ServicesToRun;

            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

            System.ServiceProcess.ServiceBase.Run(ServicesToRun);

              }

              private void InitializeComponent()

              {

                     components = new System.ComponentModel.Container();

                     this.ServiceName = "Service1";

              }

              protected override void Dispose( bool disposing )

              {

                     if( disposing )

                     {

                            if (components != null)

                            {

                                   components.Dispose();

                            }

                     }

                     base.Dispose( disposing );

              }

              protected override void OnStart(string[] args)

              {

                    

              }

             protected override void OnStop()

              {

                    

              }

       }

}

 

有必要将其结构讲解一下。其中,System.ServiceProcess就是关键所在,是引入windows service的地方。其中的OnStart()OnStop()两个函数能够被windows服务管理器或者MMC调用,进行服务的启动、停止。

二、构建一个服务

该框架提供了一个空的服务,什么也不能做。所以我们要给它添加代码。比如,我想

做一个能够扫描数据库的服务,要求每次扫描完之后间隔一秒钟,然后继续扫描。

根据上面的要求,初步设想需要一个timer类,查命名空间,发现有二个不同的timer类,他们是:

1、  System.Windows.Forms.Timer

2、  System.Timers.Timer

还有一个System.Threading,带有sleep方法

究竟该用哪个呢?

细查MSDN,会找到只有2适合,对于1来说,timer控制时间不够精确,对于线程来说,实现比较困难。

三、规划一下流程

基于该服务的要求,确定服务的流程如下:

为此,我们定义两个函数:_Scan(bool _judge)_DO_Something()

然后引入System.Timers命名空间,并且定义一个_timer对象,这些代码如下:

1using System.Timers;                       //引入System.Timers                                                                  

2public System.Timers.Timer _timer;           //定义对象_timer                                                              

3public bool _Scan(bool _judge)                                                                                                             

              {                                                                                                                                                           

                     //TODO                                                                                                                                       

              }                                                                                                                                                          

4public void _DO_Something()                                                                                                              

              {                                                                                                                                                         

                     //TODO                                                            

              }                                                                      

 

然后在InitializeComponent()里边把_timerElapsed事件添加上,代码如下:

this._timer.Elapsed += new System.Timers.ElapsedEventHandler(this._timer_Elapsed);

   定义_timer_Elapsed事件,代码如下:

private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)              

              {                                                                     

                     _timer.Interval=1000;                                                 

                  _timer.Enabled=false;                                                 

              if(_Scan()==true)                                                         

                     {                                                                 

                            _DO_Something();                                               

                     }                                                                  

                     _timer.Enabled=true;                                                 

            }                                                                    

   最后,我们不要忘记添加windows serviceinstaller,也是一个wizard,基本上不需要添加一行代码。然后编译,生成一个可执行文件。注意:因为这不是普通的可执行文件,所以不能通过双击实现运行,只能通过installutil YourServiceNameNET START YourServiceNameNET STOP YourServiceNameinstallutil/u YourServiceName来进行该服务的安装、启动、停止、暂停(可选)、卸载。最好是做成批处理文件,一劳永逸。^_^

                                                            tojike

 

 

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有