ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
News
Php Tips
Android Programming
Design Pattern By PHP
C# using Linq น่าใช้จริงๆ
C# Tips & Technique
C# Design Pattern
Linux Quick Tips
Java & JavaScript Tips
Database & SQL
ZengCode Framework Guide
Zeng Code Code
Programming
IPhone (Tips and Trick)

Download เอกสารที่น่าสนใจ

     Simple Windows Servicy by C#  (2009-12-28)

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Diagnostics;  
  6. using System.Linq;  
  7. using System.ServiceProcess;  
  8. using System.Text;  
  9. using System.Threading;  
  10.   
  11. namespace Test  
  12. {  
  13.     public partial class Service1 : ServiceBase  
  14.     {  
  15.         static private bool cmdStop = false;  
  16.   
  17.         public Service1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         protected override void OnStart(string[] args)  
  23.         {  
  24.             cmdStop = false;  
  25.             new Thread(new ThreadStart(SimpleThread)).Start();  
  26.         }  
  27.   
  28.         protected override void OnStop()  
  29.         {  
  30.             cmdStop = true;  
  31.         }  
  32.   
  33.         static private void SimpleThread()  
  34.         {  
  35.             while (cmdStop == false)  
  36.             {  
  37.                 // ทำงานไปในส่วนนี้  
  38.             }  
  39.         }  
  40.     }  


Comment

ZengCode  (28 ธันวาคม 2552)   
IP : 203.154.112.163

สร้าง Windows Service ด้วย C#

ธันวาคม 15th, 2008

เป็นการ service เพื่อทำงานในลักษณะ background process ตัวอย่างที่เห็นได้ชัดคือพวก
apache, tomcat , fax service ของ microsoft ก็ใช้งานในลักษณะนี้

เริ่มต้นจากการสร้าง windows service

 

System.IO.StreamWrite file;

protected override void OnStart(string[] args)
{
// create or open the file. Default path is "C:\windows\System32\"
file = new StreamWriter( new FileStream("ServiceTest.log", System.IO.FileMode.Append ) );
this.file.WriteLine("Starting Service");
this.file.Flush();
}

protected override void OnStop()
{
this.file.WriteLine("Stopping Service");
this.file.Flush();
this.file.Close();
}

จากนั้นให้สร้าง installer class โดยเพิ่ม using System.ServiceProcess; เข้าไปพร้อมโค้ดต่อไปนี้

ServiceInstaller si = new ServiceInstaller();
ServiceProcessInstaller spi = new ServiceProcessInstaller();

si.ServiceName = “Service1″; // this must match the ServiceName specified in WindowsService1.
si.DisplayName = “Devhood Tutorial Service”; // this will be displayed in the Services Manager.
this.Installers.Add(si);

spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem; // run under the system account.
spi.Password = null;
spi.Username = null;
this.Installers.Add(spi);

 

เพื่อให้โปรแกรมทำงานได้ ต้องทำการติดตั้ง service ด้วยวิธี “installutil.exe C:\project\WindowsService1\bin\WindowsService1.exe” และใช้ “installutil.exe /u” ในการเอา service ออก

คุณสามารถดูผลของ service ว่าได้ติดตั้งไว้แล้วจาก Computer Management ในส่วนของ services

อันนี้เอามาจากเว็บเพื่อนผมเอง http://www.sourcecode.in.th/9M/?p=50


Name
Comment
Security CodeCAPTCHA Image

easy tracking
avis car rental discount code

This page took 0.178008 seconds to load.