使用Windows服務定時去執行一個方法的三種方式


方式一:使用System.Timers.Timer定時器

public partial class Service1 : ServiceBase
    {
       
        private UnitOfWork unitOfWork;
        private System.Timers.Timer timer1;//初始化一個定時器        
        LogHelper lghelper = new LogHelper(typeof(Service1));
        public Service1()
        {
            InitializeComponent();
            unitOfWork = new UnitOfWork();
         this.timer1 = new System.Timers.Timer();
            this.timer1.Interval = 1000;//設置定時器啟動的時間間隔
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);//定時器定時執行的方法          
        }

        protected override void OnStart(string[] args)
        {
            this.timer1.Enabled = true;//服務啟動時開啟定時器
            lghelper.Info("服務啟動");
        }

        protected override void OnStop()
        {
            this.timer1.Enabled = false;//服務停止時關閉定時器
            unitOfWork.Dispose();
            lghelper.Info("服務停止");
        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.timer1.Enabled = false;//在服務運行時關閉定時器,避免在服務還沒有運行結束時達到定時器時間間隔又重頭開始運行(比如該定時器設置為5分鍾同步一次數據,當數據量很大時,5分鍾同步不完,這時達到定時器時間間隔,又會重頭開始同步,所以在服務開始運行時關閉定時器)
            lghelper.Info("服務開始運行");
            try
            {

                DoWork();
            }
            catch (Exception ex)
            {
                lghelper.Error(ex.ToString());
                lghelper.Info("服務運行失敗");
                Thread.Sleep(5000);
            }

            this.timer1.Enabled = true;//服務運行結束,重新啟動定時器
        }

       
    }

方式二:使用Task

  partial class Service2 : ServiceBase, IDisposable
    {
        LogHelper lghelper = new LogHelper(typeof(Service2));
        private CancellationTokenSource TokenSource = new CancellationTokenSource();
   protected UnitOfWork unitOfWork = new UnitOfWork();

        Task MainTask;
        public Service2()
        {
            InitializeComponent();
          

        }
        public void Dispose()
        {
            unitOfWork.Dispose();
        }

        protected override void OnStart(string[] args)
        {
            lghelper.Info("開啟服務!");
            MainTask = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    try
                    {
                      DoWork();

                    }
                    catch (Exception ex)
                    {
                        lghelper.Error(ex.ToString());

                        Thread.Sleep(60 * 60 * 1000);
                    }

                    Thread.Sleep(5 * 60 * 1000);
                }

            });

        }

        protected override void OnStop()
        {
            if (MainTask != null)
            {
                if (MainTask.Status == TaskStatus.Running) { }
                {
                    TokenSource.Cancel();
                    lghelper.Info("線程結束");
                }
            }
        }


     
    }

方式三:這個方法是看到博友的,還沒有用過,不過覺得挺方便的

http://www.cnblogs.com/ldyblogs/p/timer.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM