Most popular

วันอังคารที่ 28 มิถุนายน พ.ศ. 2554

C# สร้าง Windows Service ให้เรียกโปรแกรมอื่น

การสร้าง Windows Service ด้วย C# ผู้เขียนจะใช้ Visual Studio 2010 .Net 4.0 ในการทำตัวอย่าง
ขั้นแรกให้สร้าง Project โดยเลือกเป็น Windows Service

ในที่นี้ผู้เขียนตั้งชื่อว่า "myService"
Visual Studio จะสร้าง ไฟล์ Service1.cs และ Code ให้ชุดนึง เป็น Code การทำงาน Start และ Stop ของ Service
ผู้เขียนจะใส่คำสั่งในการเปิดโปรแกรม Notepad เข้าไปที่ OnStart ของ Service


        protected override void OnStart(string[] args)
        {
            Process.Start("notepad.exe");
        }


แต่เนื่องจากการสร้าง Windows Service จะไม่สามารถจำลองการทำงานของโปรแกรมได้ ต้องทำการติดตั้ง Service เข้าไปใน Windows ก่อนจึงจะรู้ผลการทำงาน ใน .Net จะต้องสร้าง Installer class เพื่อกำหนดค่าต่างๆในการติดตั้ง เพิียงเพิ่ม Installer Class เข้าไปใน Project แล้วเขียน Code เพิ่มดังนี้

คลิ๊กเมาส์ขวาที่ Project เลือก Add > New Item
ไปที่หมวด General และเลือก Installer Class ในที่นี้ผู้เขียนจะใช้ชื่อที่เป็นเริ่มต้นคือ Installer1.cs
จากนั้นเขียน Code ในการตั้งค่าการติดตั้งลงไปใน Installer Class (Installer1)


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;




namespace myService
{
    [RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        public Installer1()
        {
            InitializeComponent();
            ServiceInstaller si = new ServiceInstaller();
            ServiceProcessInstaller spi = new ServiceProcessInstaller();


            si.ServiceName = "myService"; // ชื่อของ Service
            si.DisplayName = "myService"; // ชื่อที่ใช้แสดงใน Service manager
            si.StartType = ServiceStartMode.Manual; //เมื่อติดตั้งแล้วให้อยู่ในสถานะ Manual
            this.Installers.Add(si);


            spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem; // กำหนดประเภทของ Service
            spi.Password = null;
            spi.Username = null;


            this.Installers.Add(spi);
        }
    }
}


เสร็จเรียบร้อยก็ให้ทำการ Build Project เท่านี้ Windows Service ก็พร้อมที่จะนำไปติดตั้งแล้ว
เพื่อความสะดวกในการติดตั้งผู้เขียนขอแนะนำให้สร้าง Batch File ในการช่วยติดตั้ง (Install) และถอนออก (Uninstall)


ภายในไฟล์ install.bat

@ECHO OFF

REM The following directory is for .NET 4.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

echo Installing myService...
echo ---------------------------------------------------
InstallUtil /i myService.exe
echo ---------------------------------------------------
echo Done.

ภายในไฟล์ uninstall.bat

@ECHO OFF

REM The following directory is for .NET 4.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

echo Installing myService...
echo ---------------------------------------------------
InstallUtil /u myService.exe
echo ---------------------------------------------------
echo Done.



เมื่อทำการรันไฟล์ install.bat จะทำการติดตั้ง myService ทันที
จากนั้นให้เข้าไปดูใน Control Panel > Administrative Tools > Services จากนั้นหาชื่อ myService แล้วเข้าไปดูที่ Properties

คลิ๊กเมาส์ปุ่มขวาแล้วเลือก properties
ใน Tab Log on ให้ติ๊กเครื่องหมายที่  Allow service to interact with desktop เพื่อให้สามารถเรียกโปรแกรมอื่นๆได้  จากนั้นกด Apply
ทดสอบด้วยการกดปุ่ม Start ใน Tab General
เมื่อ Service อยู่ในสถานะ Start ก็จะทำการเรียกให้โปรแกรม Notepad เปิดขึ้นมาทันที
เป็นตัวอย่างง่ายๆ ที่สามารถนำไปประยุคใช้ได้ ผู้เขียนหวังว่าจะมีประโยชน์สำหรับทุกท่านไม่มากก็น้อยนะครับ


  รับวางระบบงาน ระบบ Network และพัฒนาระบบ Data warehouse และ ERP ด้วยทีมงานที่มีประสบการณ์ ติดต่อได้ที่ e-mail : arrays2003@hotmail.com

ไม่มีความคิดเห็น:

แสดงความคิดเห็น