最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

用.NET創(chuàng)建Windows服務(wù)的方法第1/2頁

 更新時(shí)間:2007年03月16日 00:00:00   作者:  

譯者說明:我是通過翻譯來學(xué)習(xí)C#的,文中涉及到的有Visual Studio.NET有關(guān)操作,我都根據(jù)中文版的VS.NET顯示信息來處理的,可以讓大家不致有誤解。

作者:Mark Strawmyer

我們將研究如何創(chuàng)建一個(gè)作為Windows服務(wù)的應(yīng)用程序。內(nèi)容包含什么是Windows服務(wù),如何創(chuàng)建、安裝和調(diào)試它們。會(huì)用到System.ServiceProcess.ServiceBase命名空間的類。


什么是Windows服務(wù)?


Windows服務(wù)應(yīng)用程序是一種需要長期運(yùn)行的應(yīng)用程序,它對(duì)于服務(wù)器環(huán)境特別適合。它沒有用戶界面,并且也不會(huì)產(chǎn)生任何可視輸出。任何用戶消息都會(huì)被寫進(jìn)Windows事件日志。計(jì)算機(jī)啟動(dòng)時(shí),服務(wù)會(huì)自動(dòng)開始運(yùn)行。它們不要用戶一定登錄才運(yùn)行,它們能在包括這個(gè)系統(tǒng)內(nèi)的任何用戶環(huán)境下運(yùn)行。通過服務(wù)控制管理器,Windows服務(wù)是可控的,可以終止、暫停及當(dāng)需要時(shí)啟動(dòng)。

Windows 服務(wù),以前的NT服務(wù),都是被作為Windows NT操作系統(tǒng)的一部分引進(jìn)來的。它們?cè)赪indows 9x及Windows Me下沒有。你需要使用NT級(jí)別的操作系統(tǒng)來運(yùn)行Windows服務(wù),諸如:Windows NT、Windows 2000 Professional或Windows 2000 Server。舉例而言,以Windows服務(wù)形式的產(chǎn)品有:Microsoft Exchange、SQL Server,還有別的如設(shè)置計(jì)算機(jī)時(shí)鐘的Windows Time服務(wù)。


創(chuàng)建一個(gè)Windows服務(wù)

我們即將創(chuàng)建的這個(gè)服務(wù)除了演示什么也不做。服務(wù)被啟動(dòng)時(shí)會(huì)把一個(gè)條目信息登記到一個(gè)數(shù)據(jù)庫當(dāng)中來指明這個(gè)服務(wù)已經(jīng)啟動(dòng)了。在服務(wù)運(yùn)行期間,它會(huì)在指定的時(shí)間間隔內(nèi)定期創(chuàng)建一個(gè)數(shù)據(jù)庫項(xiàng)目記錄。服務(wù)停止時(shí)會(huì)創(chuàng)建最后一條數(shù)據(jù)庫記錄。這個(gè)服務(wù)會(huì)自動(dòng)向Windows應(yīng)用程序日志當(dāng)中登記下它成功啟動(dòng)或停止時(shí)的記錄。

Visual Studio .NET能夠使創(chuàng)建一個(gè)Windows服務(wù)變成相當(dāng)簡單的一件事情。啟動(dòng)我們的演示服務(wù)程序的說明概述如下。

1. 新建一個(gè)項(xiàng)目
2. 從一個(gè)可用的項(xiàng)目模板列表當(dāng)中選擇Windows服務(wù)
3. 設(shè)計(jì)器會(huì)以設(shè)計(jì)模式打開
4. 從工具箱的組件表當(dāng)中拖動(dòng)一個(gè)Timer對(duì)象到這個(gè)設(shè)計(jì)表面上 (注意: 要確保是從組件列表而不是從Windows窗體列表當(dāng)中使用Timer)
5. 設(shè)置Timer屬性,Enabled屬性為False,Interval屬性30000毫秒
6. 切換到代碼視圖頁(按F7或在視圖菜單當(dāng)中選擇代碼),然后為這個(gè)服務(wù)填加功能


Windows服務(wù)的構(gòu)成

在你類后面所包含的代碼里,你會(huì)注意到你所創(chuàng)建的Windows服務(wù)擴(kuò)充了System.ServiceProcess.Service類。所有以.NET方式建立的Windows服務(wù)必須擴(kuò)充這個(gè)類。它會(huì)要求你的服務(wù)重載下面的方法,Visual Studio默認(rèn)時(shí)包括了這些方法。

• Dispose – 清除任何受控和不受控資源(managed and unmanaged resources)
• OnStart – 控制服務(wù)啟動(dòng)
• OnStop – 控制服務(wù)停止

數(shù)據(jù)庫表腳本樣例

在這個(gè)例子中使用的數(shù)據(jù)庫表是使用下面的T-SQL腳本創(chuàng)建的。我選擇SQL Server數(shù)據(jù)庫。你可以很容易修改這個(gè)例子讓它在Access或任何你所選擇的別的數(shù)據(jù)庫下運(yùn)行。

CREATE TABLE [dbo].[MyServiceLog] (
   [in_LogId] [int] IDENTITY (1, 1) NOT NULL,
   [vc_Status] [nvarchar] (40)
           COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
   [dt_Created] [datetime] NOT NULL
) ON [PRIMARY]


Windows服務(wù)樣例

下面就是我命名為MyService的Windows服務(wù)的所有源代碼。大多數(shù)源代碼是由Visual Studio自動(dòng)生成的。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ServiceProcess;

namespace CodeGuru.MyWindowsService
{
  public class MyService : System.ServiceProcess.ServiceBase
  {
   private System.Timers.Timer timer1;
   /// <remarks>
   /// Required designer variable.
   /// </remarks>
   private System.ComponentModel.Container components = null;

   public MyService()
   {
       // This call is required by the Windows.Forms
       // Component Designer.
     InitializeComponent();
   }

   // The main entry point for the process
   static void Main()
   {
     System.ServiceProcess.ServiceBase[] ServicesToRun;

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

     System.ServiceProcess.ServiceBase.Run(ServicesToRun);
   }

   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
     this.timer1 = new System.Timers.Timer();
     ((System.ComponentModel.ISupportInitialize)
(this.timer1)).BeginInit();
     //
     // timer1
     //
     this.timer1.Interval = 30000;
     this.timer1.Elapsed +=
   new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
     //
     // MyService
     //
     this.ServiceName = "My Sample Service";
     ((System.ComponentModel.ISupportInitialize)
(this.timer1)).EndInit();

   }

   /// <summary>
   /// Clean up any resources being used.
   /// </summary>
   protected override void Dispose( bool disposing )
   {
     if( disposing )
     {
      if (components != null)
      {
         components.Dispose();
      }
     }
     base.Dispose( disposing );
   }

   /// <summary>
   /// Set things in motion so your service can do its work.
   /// </summary>
   protected override void OnStart(string[] args)
   {
     this.timer1.Enabled = true;
     this.LogMessage("Service Started");
   }

   /// <summary>
   /// Stop this service.
   /// </summary>
   protected override void OnStop()
   {
     this.timer1.Enabled = false;
     this.LogMessage("Service Stopped");
   }

   /*
    * Respond to the Elapsed event of the timer control
    */
   private void timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
   {
     this.LogMessage("Service Running");
   }

   /*
    * Log specified message to database
    */
   private void LogMessage(string Message)
   {
     SqlConnection connection = null;
     SqlCommand command = null;
     try
     {
      connection = new SqlConnection(
"Server=localhost;Database=SampleDatabase;Integrated
Security=false;User Id=sa;Password=;");
command = new SqlCommand(
"INSERT INTO MyServiceLog (vc_Status, dt_Created)
VALUES ('" + Message + "',getdate())", connection);
      connection.Open();
      int numrows = command.ExecuteNonQuery();
     }
     catch( Exception ex )
     {
      System.Diagnostics.Debug.WriteLine(ex.Message);
     }
     finally
     {
      command.Dispose();
      connection.Dispose();
     }
   }
  }
}

相關(guān)文章

  • 基于C#實(shí)現(xiàn)高效示波器功能

    基于C#實(shí)現(xiàn)高效示波器功能

    這篇文章介紹了用?C#實(shí)現(xiàn)示波器功能的方法,包括使用?WinForm?及多種曲線控件,闡述了原理和思路,如定義緩存數(shù)據(jù)的隊(duì)列、轉(zhuǎn)化數(shù)組刷新顯示等,還提到注意事項(xiàng)及擴(kuò)展特性,最后呼吁點(diǎn)贊支持和交流,需要的朋友可以參考下
    2024-12-12
  • C# List引用類型克隆的3種方法

    C# List引用類型克隆的3種方法

    這篇文章主要給大家介紹了關(guān)于C# List引用類型克隆的3種方法,包括反射、序列化(依賴Newtonsoft.Json) 以及序列化(BinaryFormatter)的實(shí)現(xiàn)方法,需要的朋友可以參考借鑒,下面來一起看看吧
    2019-01-01
  • Stream.Write 與 StreamWriter.Write 的不同

    Stream.Write 與 StreamWriter.Write 的不同

    Stream.Write 與 StreamWriter.Write 是我們?cè)谙蛄髦袑憯?shù)據(jù)時(shí),最常用的方法。下面就詳細(xì)講解這兩個(gè)方法。
    2013-04-04
  • C#和lua相互調(diào)用的方法教程

    C#和lua相互調(diào)用的方法教程

    lua是一種腳本語言,可以方便的移植到各種宿主語言中,并且可以支持熱更新,在游戲開發(fā)中也能當(dāng)做主要的語言來編寫游戲的邏輯,所以這篇文章主要給大家介紹了關(guān)于C#和lua相互調(diào)用的方法教程,需要的朋友可以參考下。
    2017-11-11
  • C#開啟線程的四種方式小結(jié)

    C#開啟線程的四種方式小結(jié)

    在C#中,多線程編程是處理并發(fā)操作、提高程序性能的重要手段,C#提供了多種方式來創(chuàng)建和管理線程,下面將介紹四種常用的開啟線程的方法,并附上相應(yīng)的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2024-06-06
  • C#中使用UDP通信實(shí)例

    C#中使用UDP通信實(shí)例

    這篇文章主要介紹了C#中使用UDP通信實(shí)例,非常實(shí)用的技巧,需要的朋友可以參考下
    2014-08-08
  • List轉(zhuǎn)換成DataSet實(shí)現(xiàn)代碼

    List轉(zhuǎn)換成DataSet實(shí)現(xiàn)代碼

    怎樣把List轉(zhuǎn)換成DataSet本人很是疑惑,于是搜集整理一番,需要的朋友可以參考下
    2012-12-12
  • 輕松學(xué)習(xí)C#的方法

    輕松學(xué)習(xí)C#的方法

    輕松學(xué)習(xí)C#的方法,對(duì)C#的方法感興趣的朋友可以參考本篇文章,幫助大家更靈活的運(yùn)用C#的方法
    2015-11-11
  • c#通過反射實(shí)現(xiàn)對(duì)象自動(dòng)映射的實(shí)現(xiàn)

    c#通過反射實(shí)現(xiàn)對(duì)象自動(dòng)映射的實(shí)現(xiàn)

    本文主要介紹了c#通過反射完成對(duì)象自動(dòng)映射的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-02-02
  • C# 線性插值的實(shí)現(xiàn)示例

    C# 線性插值的實(shí)現(xiàn)示例

    線性插值是針對(duì)一維數(shù)據(jù)的插值方法,本文主要介紹了C# 線性插值的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03

最新評(píng)論

桐柏县| 澄城县| 威远县| 杂多县| 阿勒泰市| 万载县| 旅游| 侯马市| 临湘市| 吉隆县| 临江市| 沙洋县| 陈巴尔虎旗| 康定县| 汉沽区| 花莲县| 开远市| 太谷县| 祁门县| 来凤县| 图木舒克市| 昭平县| 梨树县| 绍兴市| 滁州市| 洪江市| 德保县| 金阳县| 德钦县| 金堂县| 镇远县| 遵义市| 岑溪市| 乐都县| 河北省| 永和县| 敦煌市| 孙吴县| 昌吉市| 扶绥县| 武川县|