首页 > 编程 > .NET > 正文

用.net创建windows service的总结(C#代码)tojike(原作)

2024-07-10 13:01:46
字体:
来源:转载
供稿:网友
用.net创建windows service的总结    tojike(原作)  
  
关键字     windows service
  


前言

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对象,这些代码如下:

1、using system.timers;                       //引入system.timers                                                                   

2、public system.timers.timer _timer;           //定义对象_timer                                                              

3、public bool _scan(bool _judge)                                                                                                              

              {                                                                                                                                                            

                     //todo                                                                                                                                        

              }                                                                                                                                                           

4、public void _do_something()                                                                                                               

              {                                                                                                                                                          

                     //todo                                                             

              }                                                                      



然后在initializecomponent()里边把_timer的elapsed事件添加上,代码如下:

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 service的installer,也是一个wizard,基本上不需要添加一行代码。然后编译,生成一个可执行文件。注意:因为这不是普通的可执行文件,所以不能通过双击实现运行,只能通过installutil yourservicename、net start yourservicename、net stop yourservicename、installutil/u yourservicename来进行该服务的安装、启动、停止、暂停(可选)、卸载。最好是做成批处理文件,一劳永逸。^_^


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表