1. 程式人生 > >C# 編寫Windows Service(windows服務程序)

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

init override services ges 計算 public control etime 令行

  • 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服務程序)