C# .net 定时器,多线程实现定时器

using System;
using System.Threading;

namespace TimerRunDemo
{
    class Program
    {
        /// <summary>  
        /// 声明一个线程实例  
        /// </summary>  
        public static Thread mythread;

        #region 计算剩余时间
        private static int CompareDate(string synchroDateStr)
        {
            long nowDT = DateTime.Now.Ticks / 10000;
            DateTime synchroDate = Convert.ToDateTime(synchroDateStr);
            long synchroDT = synchroDate.Ticks / 10000;
            int sleepDT = -1;
            if (synchroDT > nowDT)
            {
                sleepDT = Convert.ToInt32(synchroDT - nowDT);
            }
            if (sleepDT != -1)
            {
                Console.WriteLine("当前时间的毫秒数:" + nowDT);
                Console.WriteLine("执行同步的时间的毫秒数:" + synchroDT);
                Console.WriteLine("剩余时间的毫秒数:" + sleepDT);
                Console.WriteLine("剩余时间的秒数:" + sleepDT / 1000);
                Console.WriteLine("剩余时间的分钟数:" + sleepDT / 1000 / 60);
                Console.WriteLine("剩余时间的小时数:" + sleepDT / 1000 / 60 / 60);
                Console.WriteLine("执行同步的时间:" + synchroDateStr);
            }
            return sleepDT;
        }

        #endregion 计算剩余时间

        #region 主程序启动

        //开始运行  
        public static void Main(string[] args)
        {
            mythread = new Thread(new ThreadStart(work));
            mythread.Start();
        }


        public static void work()
        {
            while (true)
            {
                string configSynchroTime;
                int sleepMilliseconds;
                configSynchroTime = "60";
                sleepMilliseconds = Timer_Seconds(configSynchroTime);
                Thread.Sleep(sleepMilliseconds);
                dowork();
                //configSynchroTime = "15:06|15:38|15:39";
                //sleepMilliseconds = Timer_AtSpecificTimes(configSynchroTime);
                //Thread.Sleep(sleepMilliseconds);
                dowork();//方法放到后边,可以避免在系统启动的时候立即执行
            }
        }
        /// <summary>
        /// 按频率 执行
        /// 1分钟=60秒
        /// </summary>
        /// <param name="seconds"></param>
        /// <returns></returns>
        private static int Timer_Seconds(string seconds)
        {
            string synchroDateStr = DateTime.Now.AddSeconds(double.Parse(seconds)).ToString();
            int sleepMilliseconds = CompareDate(synchroDateStr);
            return sleepMilliseconds;
        }
        /// <summary>
        /// 在特定的时间 执行
        /// </summary>
        /// <param name="specificTimes"></param>
        /// <returns></returns>
        private static int Timer_AtSpecificTimes(string specificTimes)
        {
            DateTime syncTime;
            DateTime currentDate = DateTime.Now.Date;
            int sleepMilliseconds = 0;
            var synchroTimes = specificTimes.Split(new char[] { '|' });
            for (int i = 0; i < synchroTimes.Length; i++)
            {
                syncTime = Convert.ToDateTime(synchroTimes[i]);
                string synchroDateStr = currentDate.AddHours(syncTime.Hour).AddMinutes(syncTime.Minute).ToString();
                sleepMilliseconds = CompareDate(synchroDateStr);
                if (sleepMilliseconds != -1) break;
            }
            if (sleepMilliseconds == -1)
            {
                //已经过期的情况增加1天
                currentDate = currentDate.AddDays(1);
                for (int i = 0; i < synchroTimes.Length; i++)
                {
                    syncTime = Convert.ToDateTime(synchroTimes[i]);
                    string synchroDateStr = currentDate.AddHours(syncTime.Hour).AddMinutes(syncTime.Minute).ToString();
                    sleepMilliseconds = CompareDate(synchroDateStr);
                    if (sleepMilliseconds != -1) break;
                }
            }
            return sleepMilliseconds;
        }

        public static void dowork()
        {
            Console.WriteLine("123");
        }

        #endregion
    }
}

展开阅读全文

没有更多推荐了,返回首页