public partial class CareEye : ServiceBase
{
private Thread MainThread;
public CareEye()
{
InitializeComponent();
this.ServiceName = "CareEye";
MainThread = new Thread(ThreadFunc);
}
protected override void OnStart(string[] args)
{
MainThread.Start();
}
protected override void OnStop()
{
MainThread.Abort();
}
public static void ThreadFunc()
{
int LastHour = DateTime.Now.Hour;
while (true)
{
Thread.Sleep(60000); // 每分鐘檢查一次
if (DateTime.Now.Hour - 1 == LastHour)
{
MessageBox.Show("為了愛護您的眼睛,請您暫時休息5分鐘並向遠處眺望!", "警告",
MessageBoxButtons.OK, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
LastHour = DateTime.Now.Hour;
}
}
}
}
ProjectInstaller.cs,包含 serviceProcessInstaller1、serviceInstaller1。serviceProcessInstaller1.Account = LocalSystem。serviceInstaller1.Parent = serviceProcessInstaller1,設定 ServiceName 為服務顯示名稱。W2.exe(取較短檔名者),複製到易存取路徑(如 F:\\)。cd C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727InstallUtil f:\\W2.exeInstallUtil f:\\W2.exe -u開啟「服務」管理工具 (services.msc),找到「CareEye」啟動即可。
MessageBox),需在 serviceProcessInstaller1 勾選「允許服務與桌面互動」或程式碼設定 MessageBoxOptions.DefaultDesktopOnly。EventLog 記錄而非 MessageBox。