日期:2012-09-13  浏览次数:20377 次

用.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)

                            {