国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

用.net創建windowsservice的總結

2019-11-18 11:17:52
字體:
來源:轉載
供稿:網友

  前言
  
  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來進行該服務的安裝、啟動、停止、暫停(可選)、卸載。最好是做成批處理文件,一勞永逸。^_^
  
  tojike(原作)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西乌珠穆沁旗| 上饶市| 三门县| 澜沧| 淄博市| 京山县| 青州市| 闸北区| 攀枝花市| 凤凰县| 平湖市| 南皮县| 卢氏县| 土默特左旗| 玉屏| 疏勒县| 泗水县| 平潭县| 探索| 襄垣县| 类乌齐县| 聂拉木县| 南平市| 望奎县| 巨野县| 社会| 浏阳市| 大洼县| 乳源| 乌兰浩特市| 行唐县| 萝北县| 阿拉善盟| 兴城市| 新竹市| 临安市| 文山县| 新闻| 堆龙德庆县| 连城县| 株洲市|