首頁 > 未分類 > 用C#寫Windows服務(Wndows service)含Timer及安裝專案

用C#寫Windows服務(Wndows service)含Timer及安裝專案

2009年5月11日 Austin 發表評論 閱讀評論

Step 1. 建立專案
檔案 => 新增 => 專案
選擇 Visual C# => Windows => Windows 服務 => 填寫相關訊息
建立專案

Service的屬性設定
ServiceName(供系統辨別服務的名稱)
Service的屬性設定

Step 2. 加入安裝程式
加入安裝程式

Step3. ServiceProcessInstaller的屬性設定
Account(執行權限):LocalSystem > NetworkService(非匿名) > LocalService(匿名)
ServiceProcessInstaller的屬性設定

Step 4. ServiceInstaller的屬性設定
Description(顯示在服務管理的描述)
DisplayName(顯示在服務管理的名稱)
ServiceName(跟上面的一樣,可是又好像有點不同@@)
StartType(服務狀態):Automatic為開機時會自動執行(第一次安裝後還是要手動)
ServiceInstaller的屬性設定

Step 5. 編輯Service1.cs的程式碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

// 加入Timers的namespace
using System.Timers;

namespace My_WinSwevice
{
    public partial class Service1 : ServiceBase
    {
        // new一個timer
        Timer timer1 = new Timer();

        public Service1()
        {
            InitializeComponent();
        }

        // 啟動服務的程式
        protected override void OnStart(string[] args) {
            try {
                // 設定timer1的Handler
                timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);

                // timer1的時間間隔(單位: ms)
                timer1.Interval = 5000;

                // 啟動timer1
                timer1.Start();

                EventLog.WriteEntry(this.ServiceName + " 服務啟動...", EventLogEntryType.Information);
            } catch(Exception ex) {
                this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
        }

        // 停止服務所需執行的終止程式
        protected override void OnStop() {
            try {
                // 關閉timer1
                timer1.Stop();

                EventLog.WriteEntry(this.ServiceName + " 服務停止...", EventLogEntryType.Information);
            } catch (Exception ex) {
                this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
        }

        // timer1的Handler
        private void timer1_Elapsed(object sender, EventArgs e) {
            try {
                // Do something here!

                EventLog.WriteEntry(this.ServiceName + " 這是計時器記錄...", EventLogEntryType.Information);
            } catch (Exception ex) {
                this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
        }
    }
}
Windows記錄
Windows記錄
Windows記錄(Timer)
Windows記錄(Timer)

Step 7. 安裝
最快的方式是使用C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe My_WinSwevice.exe(加入參數 /u 為移除)

另一個則是建立安裝專案
檔案 => 新增 => 專案
選擇 其他專案類型 => 安裝和部署 => 安裝專案 => 填寫其他訊息(方案要選"加入至方案")
新增安裝專案

方案總管 => SetupService => 加入 => 專案輸出
然後從下拉選單選擇My_WinSwevice => 點選主要輸出 => 確定
加入專案輸出群組

方案總管 => SetupService => 檢視 => 自訂動作
自訂動作按右鍵 => 加入自訂動作
然後從下拉選單中選擇應用程式資料夾 => 點選主要輸出 從My_WinSwevice(作用中) => 確定
加入自訂動作

然後先建置My_WinSwevice,再建置SetupService
便可以在SetupService的目錄中找到Setup.exeSetupService.msi

參考資料:[C#.NET][VB.NET] 如何建立 Windows 服務 Service 專案Windows Service with Timer ?Using Timers in a C# Windows Service如何在 Visual C# 中建立 Windows 服務應用程式的安裝專案

Categories: 未分類 Tags: , ,
  1. 目前尚無任何的評論。
  1. 目前尚無任何 trackbacks 和 pingbacks。