TimeExit 界面无点击定时退出类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Share
{
    public class TimeExit
    {
        //单例模式
        private static readonly TimeExit m_TimeExit = new TimeExit();
        public static TimeExit Instance() { return m_TimeExit; }
        private TimeExit()
        {
            timer = new System.Timers.Timer(1000);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件;

            timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
        }
        /// <summary>
        /// 开始时间
        /// </summary>
        public static DateTime StartTime = DateTime.Now;
        /// <summary>
        /// 设置定时退出时间间隔
        /// </summary>
        public int Time = 60;
        /// <summary>
        /// 剩余时间
        /// </summary>
        public int TimeRemain = 60;

        private System.Timers.Timer timer;
        private List<Label> LableLst = new List<Label>();
        ///// <summary>
        ///// 定时要关闭的窗口集合
        ///// </summary>
        public List<Form> FormLst = new List<Form>();
        //定义委托
        public delegate void IntervalHandle(object sender, EventArgs e);
        //定义事件
        /// <summary>
        /// 到时间执行的事件
        /// </summary>
        public event IntervalHandle IntervalClicked;
        /// <summary>
        /// 开启定时退出器
        /// </summary>
        public void Start()
        {
            timer.Enabled = true;
            StartTime = DateTime.Now;
        }

        private void theout(object sender, EventArgs e)
        {
            TimeSpan lastTimeSpans = DateTime.Now - StartTime;
            TimeRemain = Time - (int)lastTimeSpans.TotalSeconds;
            ///全部界面的定时显示都改
            foreach (var item in LableLst)
            {
                item.Invoke(new Action(() =>
                {
                    item.Text = TimeRemain.ToString();
                }));
            }
            if (lastTimeSpans.TotalSeconds >= Time)
            {
                timer.Enabled = false;
                Interval();
            }
        }
        /// <summary>
        /// 关闭定时退出器
        /// </summary>
        public void Stop()
        {
            timer.Enabled = false;
            HideText();
        }
        /// <summary>
        /// 到时间执行的事件
        /// </summary>
        private void Interval()
        {
            foreach (var item in FormLst)
            {
                item.Close();
            }
            IntervalClicked?.Invoke(this, null);
        }
        public void Add(Label lb)
        {
            LableLst.Add(lb);
        }

        public void Romove(Label lb)
        {
            LableLst.Remove(lb);
        }

        private void HideText()
        {
            ///全部界面的定时显示都改
            foreach (var item in LableLst)
            {
                item.Invoke(new Action(() =>
                {
                    item.Text = "";
                }));
            }
        }

        public void MouseUp(object sender, MouseEventArgs e)
        {
            StartTime = DateTime.Now;
        }
    }
}
原文地址:https://www.cnblogs.com/lsgsanxiao/p/9020292.html