C# 編寫Windows Service(windows服務程式)

來源:互聯網
上載者:User

標籤:admin   images   image   服務無法啟動   space   .net   包括   技術分享   資訊   

  •  Windows Service簡介:

一個Windows服務程式是在Windows作業系統下能完成特定功能的可執行檔應用程式。Windows服務程式雖然是可執行檔,但是它不像一般的可執行檔通過雙擊就能開始運行了,它必須有特定的啟動方式。這些啟動方式包括了自動啟動和手動啟動兩種。對於自動啟動的Windows服務程式,它們在Windows啟動或是重啟之後使用者登入之前就開始執行了。只要你將相應的Windows服務程式註冊到服務控制管理員(Service Control Manager)中,並將其啟動類別設為自動啟動就行了。而對於手動啟動的Windows服務程式,你可以通過命令列工具的NET START 命令來啟動它,或是通過控制台中管理工具下的服務一項來啟動相應的Windows服務程式。

同樣,一個Windows服務程式也不能像一般的應用程式那樣被終止。因為Windows服務程式一般是沒有使用者介面的,所以你也要通過命令列工具或是下面圖中的工具來停止它,或是在系統關閉時使得Windows服務程式自動停止。因為Windows服務程式沒有使用者介面,所以基於使用者介面的API函數對其是沒有多大的意義。為了能使一個Windows服務程式能夠正常並有效在系統內容下工作,程式員必須實現一系列的方法來完成其服務功能。Windows服務程式的應用範圍很廣,典型的Windows服務程式包含了硬體控制、應用程式監視、系統級應用、診斷、報告、Web和檔案系統服務等功能。

和Windows服務程式相關的命名空間涉及到以下兩個:System.ServiceProcess System.Diagnostics

  • 用C#建立Windows服務的步驟:

1.建立Windows Service項目

從Visual C# 工程中選取 Windows 服務(Windows Service)選項,給工程一個新檔案名稱,然後點擊 確定。

2.向服務中函數功能實現

 OnStart函數在啟動服務時執行,OnStop函數在停止服務時執行。在這裡,當啟動和停止服務時,向一個文字檔中寫入一些文字資訊,代碼如下:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace MyService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WindowsService: Service Started" + DateTime.Now.ToString() + "\n");

sw.Flush();
sw.Close();
fs.Close();
}

//protected override void OnContinue()
//{
// base.OnContinue();
//}

//protected override void OnPause()
//{
// base.OnPause(); // father class method inherit
//}

//protected override void OnShutdown()
//{
// base.OnShutdown();
//}

protected override void OnStop()
{
FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WindowsService: Service Stopped" + DateTime.Now.ToString() + "\n");
sw.Flush();
sw.Close();
fs.Close();

}
}
}

 

4.回到設計視窗點右鍵選擇-添加安裝程式 -產生serviceInstaller1和 serviceProcessInstaller1兩個組件 
把serviceInstaller1的屬性ServiceName改寫為你的服務程式名,並把啟動模 式設定為AUTOMATIC  
把serviceProcessInstaller1的屬性account改寫為 LocalSystem  


5.編譯連結產生服務程式

通過從產生菜單中選擇產生來產生項目。

 

6.安裝服務

用.net framework工具INSTALLUTIL安裝服務程式即可。

用項目的輸出作為參數,從命令列運行 InstallUtil.exe。在命令列中輸入下列代碼: 
installutil yourproject.exe

Hint: a windows service must first be installed using installutil.exe and then started with the serviceExplorer, windows Services Administrative tool or the NET START command.

 

7.卸載服務

用項目的輸出作為參數,從命令列運行 InstallUtil.exe。

installutil /u yourproject.exe

 

如上服務程式運行結果:

 

  • 補充:

1.Service啟動屬性:

        Manual      服務安裝後,必須手動啟動。

        Automatic    每次電腦重新啟動時,服務都會自動啟動。

        Disabled     服務無法啟動。

 

2.建立的Service項目,其中各屬性的含義(設計檢視->右鍵屬性):

  Autolog 是否自動寫入系統的記錄檔

  CanHandlePowerEvent 服務時候接受電源事件

  CanPauseAndContinue 服務是否接受暫停或繼續啟動並執行請求

  CanShutdown 服務是否在運行它的電腦關閉時收到通知,以便能夠調用 OnShutDown 過程

  CanStop 服務是否接受停止啟動並執行請求

  ServiceName 服務名

 

3. 也可以在系統服務管理員中,設定相應Service的屬性或啟動方式等

電腦管理 -> 服務和應用程式  -> 服務  -> ...

 

 

本文轉載自 http://www.cnblogs.com/bluestorm/p/3510398.html

C# 編寫Windows Service(windows服務程式)

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.