C# Timer vs Thread in Service

前端 未结 4 2114
灰色年华
灰色年华 2021-02-09 05:56

I have a Service that hits a database every 10 sec and gets the data if there is any. The thing is that processing this data can take up to 30 sec. If I use a Timer with 10 sec

4条回答
  •  一向
    一向 (楼主)
    2021-02-09 06:49

    Did you try to set Timer property auto reset to false, and enabling timer again when process of refreshing data is over

    using System;
    
    public class PortChat
    {
        public static System.Timers.Timer _timer;
        public static void Main()
        {
    
            _timer = new System.Timers.Timer();
            _timer.AutoReset = false;
            _timer.Interval = 100;
            _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
            _timer.Enabled = true;
            Console.ReadKey();
        }
    
        static void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //Do database refresh
            _timer.Enabled = true;
        }
    }
    

提交回复
热议问题