C#中定时器的用法

 static void Main(string[] args)
        {
//
            //AddInventory();
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Enabled = true;
            timer.Interval = 60000;
            timer.Start();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(T_Elapsed);
            Console.Read();
        }

   private static void T_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            int intHour = e.SignalTime.Hour;
            int intMinute = e.SignalTime.Minute;
            // int intSecond = e.SignalTime.Second;
            //定制刷新库的时间
            int iHour = Convert.ToInt32(ConfigurationManager.AppSettings["Hours"].ToString());
            int iMinute = Convert.ToInt32(ConfigurationManager.AppSettings["Mininus"].ToString());

            // int iSecond = 00;
            if (intHour == iHour && intMinute == iMinute)
            {
                AddInventory();
            }
        }
View Code

实现每隔一分钟执行一次,当发现当前时间与设置的时间一样时,自动执行需要执行的方法,适合每天需要定时执行一遍的需求

posted @ 2016-08-05 13:50  笨笨的菜鸟  阅读(4417)  评论(0编辑  收藏  举报