用.net建立windows service的總結(C#代碼)tojike(原作)

來源:互聯網
上載者:User
window|建立 用.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來進行該服務的安裝、啟動、停止、暫停(可選)、卸載。最好是做成批次檔,一勞永逸。^_^



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。