眼睛保护程序

     近来,由于面对屏幕时间过长,眼睛很是不舒服,想写个自动提示工作时间的服务程序来提醒自己.于是上网搜索一通,找到了一位仁兄写的03代码,移植下,就这样的剽窃了他的成果,在此先谢过了.不要追究责任哦.其实很简单的一个服务程序.就是隔一小时弹出个信息框告诫下自己要注意休息下.大概步骤是:创建一个WINDOWS服务程序,命名为:EyeService.引用下 System.Windows.Forms;用以弹出信息框.再using System.Threading;即可.源代码如下:
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Diagnostics;
 6using System.ServiceProcess;
 7using System.Text;
 8using System.Windows.Forms;
 9using System.Threading;
10
11namespace EyeService
12{
13    public partial class EyeService : ServiceBase
14    {
15        private Thread MainThread;
16
17        public EyeService()
18        {
19            InitializeComponent();
20            MainThread = new Thread(new ThreadStart(ThreadFunc));
21            MainThread.Priority = ThreadPriority.Lowest;
22        }

23
24        protected override void OnStart(string[] args)
25        {
26            // TODO: 在此处添加代码以启动服务。
27            MainThread.Start();
28        }

29
30        protected override void OnStop()
31        {
32            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
33            MainThread.Abort();
34        }

35        /// <summary>
36        /// 保护方法
37        /// </summary>

38        public static void ThreadFunc()
39        {
40            int LastHour = DateTime.Now.Hour;
41            while (true)
42            {
43                System.Threading.Thread.Sleep(60000);
44                if (DateTime.Now.Hour - 1 == LastHour)
45                {
46                    MessageBox.Show("为了保护您的眼睛,请您暂时休息5分钟并向远处眺望!""警告", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
47                    LastHour = DateTime.Now.Hour;
48                }

49            }

50        }

51    }

52}
添加安装程序,将serviceInstaller1启动类型改为:Automatic以便开机即运行此服务.其他属性根据个人爱好更改即可.最后安装下此服务后,在控制面板的服务列表中就可以看到了.

运行一小时后弹出信息框:
原文地址:https://www.cnblogs.com/jiangshaofen/p/822434.html