winform 定时提醒软件(小记)

TimeNotifier.cs
  
   
  
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TimeNotifier
{
    public partial class TimeNotifier : Form
    {
        /**//// <summary>
        /// Const
    /// </summary>
       public class ConstDefs
       {
            internal static int DefaultSlot = 1800;
           internal static int OneThousant = 1000;
      }
  
        /**//// <summary>
       /// Constructor
        /// </summary>
       public TimeNotifier()
      {
            InitializeComponent();
           SetDefault();
           RegisterEvent();
            TimeReminder();
        }
  
       /**//// <summary>
        /// Exit
      /// </summary>
      /// <param name="sender"></param>
       /// <param name="e"></param>
       private void exitToolStripMenuItem_Click(object sender, EventArgs e)
      {
           this.newTimeThread.Abort();
           Application.Exit();
       }
  
      /**//// <summary>
       /// Reset
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
      private void resetToolStripMenuItem_Click(object sender, EventArgs e)
      {
            this.Show();
        }
  
        private void btnCancel_Click(object sender, EventArgs e)
       {
          this.Hide();
       }
    }
  
TimeNotifier.event.cs
  
   
  
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
  
namespace TimeNotifier
{
   public partial class TimeNotifier
    {
       /**//// <summary>
        /// 注册事件.
      /// </summary>
     private void RegisterEvent()
       {
            this.Closing += new System.ComponentModel.CancelEventHandler(Tray_Closing);
            this.Apply.Click += new EventHandler(Apply_Click);
      }
  
       /**//// <summary>
       /// Closing
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void Tray_Closing(object sender, CancelEventArgs e)
       {
           this.Hide();
           e.Cancel = true;
       }
  
       /**//// <summary>
       /// Reset
        /// </summary>
        /// <param name="sender"></param>
       /// <param name="e"></param>
       private void Apply_Click(object sender, EventArgs e)
        {
            this.timeSlot = Int32.Parse(this.textBox1.Text);
            this.newTimeThread.Abort();
           this.TimeReminder();
       }
  
  
       /**//// <summary> 
       /// Set Tray 
        /// </summary> 
        private void SetDefault()
        {
            timeSlot = ConstDefs.DefaultSlot;
            this.textBox1.Text = timeSlot.ToString();
        }
  
       /**//// <summary>
        /// 启动新线程.
       /// </summary>
        private void TimeReminder()
       {
           if (newTimeThread != null)
           {
               newTimeThread.Abort();
           }
           newTimeThread = new Thread(new ThreadStart(TimeNotice));
           newTimeThread.Start();
       }
  
       private void TimeNotice()
       {
           while (true)
           {
                Thread.Sleep(this.timeSlot * ConstDefs.OneThousant);
               MessageBox.Show("Time to have a rest" + DateTime.Now.ToString());
  
           }
       }
    }
  
  
  
TimeNotifier.Designer.cs
  
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
  
namespace TimeNotifier
{
    public partial class TimeNotifier
  {
        /**//// <summary> 
       /// Required designer variable. 
       /// </summary> 
        private System.ComponentModel.IContainer components = null;
        private NotifyIcon TrayIcon;
        private ContextMenu TrayMenu;
        private MenuItem subMenuExit;
        private MenuItem subMenuReset;
       private int timeSlot;
       private Thread newTimeThread;
  
       /**//// <summary> 
       /// Clean up any resources being used. 
        /// </summary> 
        /// <param name="disposing">true if managed resources should be disposed; otherwise, 
  
false.</param> 
        protected override void Dispose(bool disposing)
       {
           if (disposing && (components != null))
           {
               components.Dispose();
           }
          base.Dispose(disposing);
        }
  
        Windows Form Designer generated code#region Windows Form Designer generated code
  
        /**//// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor. 
        /// </summary> 
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
          System.ComponentModel.ComponentResourceManager resources = new 
  
System.ComponentModel.ComponentResourceManager(typeof(TimeNotifier));
            this.Apply = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
          this.gbSetting = new System.Windows.Forms.GroupBox();
           this.label1 = new System.Windows.Forms.Label();
           this.lblInfo = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
          this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.resetToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
           this.gbSetting.SuspendLayout();
           this.contextMenuStrip1.SuspendLayout();
           this.SuspendLayout();
           // 
            // Apply
           // 
            this.Apply.Location = new System.Drawing.Point(47, 173);
           this.Apply.Name = "Apply";
           this.Apply.Size = new System.Drawing.Size(75, 25);
           this.Apply.TabIndex = 1;
           this.Apply.Text = "OK";
          this.Apply.UseVisualStyleBackColor = true;
           // 
           // btnCancel
            // 
          this.btnCancel.Location = new System.Drawing.Point(151, 173);
           this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 25);
            this.btnCancel.TabIndex = 2;
          this.btnCancel.Text = "Cancel";
          this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
           // 
           // gbSetting
            // 
           this.gbSetting.Controls.Add(this.label1);
          this.gbSetting.Controls.Add(this.lblInfo);
           this.gbSetting.Controls.Add(this.textBox1);
           this.gbSetting.Location = new System.Drawing.Point(-2, 2);
           this.gbSetting.Name = "gbSetting";
           this.gbSetting.Size = new System.Drawing.Size(274, 85);
           this.gbSetting.TabIndex = 4;
           this.gbSetting.TabStop = false;
           this.gbSetting.Text = "设置";
           // 
           // label1
          // 
           this.label1.AutoSize = true;
           this.label1.Location = new System.Drawing.Point(124, 52);
           this.label1.Name = "label1";
          this.label1.Size = new System.Drawing.Size(19, 13);
            this.label1.TabIndex = 6;
           this.label1.Text = "秒";
            // 
           // lblInfo
           // 
            this.lblInfo.AutoSize = true;
            this.lblInfo.Location = new System.Drawing.Point(47, 21);
            this.lblInfo.Name = "lblInfo";
            this.lblInfo.Size = new System.Drawing.Size(100, 13);
            this.lblInfo.TabIndex = 5;
            this.lblInfo.Text = "提醒间隔时间(秒):";
            // 
            // textBox1
           // 
            this.textBox1.Location = new System.Drawing.Point(49, 49);
            this.textBox1.Name = "textBox1";
          this.textBox1.Size = new System.Drawing.Size(69, 20);
            this.textBox1.TabIndex = 4;
          // 
           // notifyIcon1
            // 
           this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
           this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject
  
("notifyIcon1.Icon")));
           this.notifyIcon1.Text = "定时提醒";
           this.notifyIcon1.Visible = true;
           this.notifyIcon1.DoubleClick += new System.EventHandler
  
(this.resetToolStripMenuItem_Click);
           // 
          // contextMenuStrip1
          // 
           this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
           this.resetToolStripMenuItem,
            this.exitToolStripMenuItem});
           this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.RenderMode = 
  
System.Windows.Forms.ToolStripRenderMode.Professional;
           this.contextMenuStrip1.Size = new System.Drawing.Size(153, 70);
           // 
            // resetToolStripMenuItem
            // 
            this.resetToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject
  
("resetToolStripMenuItem.Image")));
           this.resetToolStripMenuItem.Name = "resetToolStripMenuItem";
           this.resetToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.resetToolStripMenuItem.Text = "Reset";
            this.resetToolStripMenuItem.Click += new System.EventHandler
  
(this.resetToolStripMenuItem_Click);
           // 
            // exitToolStripMenuItem
           // 
           this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.exitToolStripMenuItem.Text = "Exit";
           this.exitToolStripMenuItem.Click += new System.EventHandler
  
(this.exitToolStripMenuItem_Click);
            // 
            // TimeNotifier
           // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.ClientSize = new System.Drawing.Size(272, 224);
          this.Controls.Add(this.gbSetting);
            this.Controls.Add(this.btnCancel);
           this.Controls.Add(this.Apply);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MinimizeBox = false;
            this.Name = "TimeNotifier";
           this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
           this.Text = "定时设置";
            this.gbSetting.ResumeLayout(false);
           this.gbSetting.PerformLayout();
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);
  
        }
  
          
  
      #endregion
  
        private Button Apply;
       private Button btnCancel;
        private GroupBox gbSetting;
        private Label lblInfo;
        private TextBox textBox1;
        private Label label1;
      private NotifyIcon notifyIcon1;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem exitToolStripMenuItem;
        private ToolStripMenuItem resetToolStripMenuItem;
  
   }
}

能够设定提醒的时间间隔,以秒记,我一般设置为1800,半个小时,默认为半个小时。
设定后点击ok,关闭主窗口,任务栏会有个托盘。

实现原理:

启动一个新线程,让其sleep所设置的时间间隔的长度,然后弹出对话框。

下面是代码文件:只有一个类TimeNotifier

有三个PartialClass  TimeNotifier.cs,  TimeNotifier.event.cs,TimeNotifier.Designer.cs

原文地址:https://www.cnblogs.com/wjmo520/p/2741942.html