MainThread=new Thread(new ThreadStart(ThreadFunc)); MainThread.Priority=ThreadPriority.Lowest; |
public static void ThreadFunc() { int LastHour=DateTime.Now.Hour; while (true) { System.Threading.Thread.Sleep(60000); if (DateTime.Now.Hour-1==LastHour) { MessageBox.Show("為了愛護您的眼睛,請您暫時休息5分鐘并向遠處眺望!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); LastHour=DateTime.Now.Hour; } } } |
installutil CareEye.exe installutil /u CareEye.exe |
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Threading; using System.Windows.Forms; namespace CareEye { public class CareEye : System.ServiceProcess.ServiceBase { private Thread MainThread; /// <summary> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.Container components = null; public CareEye() { // 該調用是 Windows.Forms 組件設計器所必需的。 InitializeComponent(); // TODO: 在 InitComponent 調用后添加任何初始化 MainThread=new Thread(new ThreadStart(ThreadFunc)); MainThread.Priority=ThreadPriority.Lowest; } // 進程的主入口點 static void Main() { //System.ServiceProcess.ServiceBase[] ServicesToRun; // 同一進程中可以運行多個用戶服務。若要將 //另一個服務添加到此進程,請更改下行 // 以創建另一個服務對象。例如, // // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new CareEye(), new MySecondUserService()}; // //ServicesToRun = new System.ServiceProcess.ServiceBase[] { new CareEye() }; System.ServiceProcess.ServiceBase.Run(new CareEye()); } /// <summary> /// 設計器支持所需的方法 - 不要使用代碼編輯器 /// 修改此方法的內容。 /// </summary> private void InitializeComponent() { // // CareEye // this.ServiceName = "CareEye"; } /// <summary> /// 清理所有正在使用的資源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } /// <summary> /// 設置具體的操作,以便服務可以執行它的工作。 /// </summary> protected override void OnStart(string[] args) { // TODO: 在此處添加代碼以啟動服務。 MainThread.Start(); } /// <summary> /// 停止此服務。 /// </summary> protected override void OnStop() { // TODO: 在此處添加代碼以執行停止服務所需的關閉操作。 MainThread.Abort(); } public static void ThreadFunc() { int LastHour=DateTime.Now.Hour; while (true) { System.Threading.Thread.Sleep(60000); if (DateTime.Now.Hour-1==LastHour) { MessageBox.Show("為了愛護您的眼睛,請您暫時休息5分鐘并向遠處眺望!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly); LastHour=DateTime.Now.Hour; } } } } } |
原文轉自:http://www.anti-gravitydesign.com