• 使用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

  • 相关阅读:
    FNV与FNV-1a Hash算法说明【转】
    FNV哈希算法【转】
    git超详细教程【转】
    Git基础篇【转】
    Notepad++中常用的插件【转】
    美化博客园界面(让您的博客更加赏心悦目)希望对您有用【转】
    scanf()总结--从网上收来的,感觉很好,用来提醒自己,c语言真是博大精深!!【转】
    机器人程序设计——之如何正确入门ROS | 硬创公开课(附视频/PPT)【转】
    ROS学习网址【原创】
    机器人操作系统ROS Indigo 入门学习(1)——安装ROS Indigo【转】
  • 原文地址:https://www.cnblogs.com/stubborn-donkey/p/7656284.html
Copyright © 2020-2023  润新知