欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# .net中的简单Job

程序员文章站 2022-04-09 20:16:46
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using Syst... ......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Timers;

namespace TestJobs
{
    class Program
    {
        private static readonly CronMon CronMon = new CronMon();
        static void Main(string[] args)
        {
            Start();
            Console.ReadKey();
        }
        public static void Start()
        {
            CronMon.AddJob(new Job());
            CronMon.AddJob(new Job(10));
            CronMon.AddJob(new Job(5));
            CronMon.Start();
        }
    }
    class Job:CronJobBase
    {
        private int _span = 0;
        public Job()
        {
        }
        public Job (int span)
            :base(span)
        { this._span = span; }
        protected override void Execute()
        {
            if (_span == 0)
                Console.WriteLine("我是Job,我使用默认时间执行!"+DateTime.Now.ToString("s"));
            else
                Console.WriteLine("我是Job,我间隔{0}s执行!{1}", _span, DateTime.Now.ToString("s"));
        } 
    }

    interface ICronJob
    {
        void Run(DateTime dateTime);
        void Abort();
    }
    class CronJobBase : ICronJob
    {
        private readonly object _object = new object();
        private  Thread _thread;
        private DateTime _runTime = DateTime.Now;
        private  int _span=3;//时间间隔 3s
        public CronJobBase()
        {

        }
        public CronJobBase(int span)
        {
            _span = span;
        }
        public void Run(DateTime dateTime)
        {
            if (_thread != null && _thread.ThreadState == ThreadState.Running)
                return;
            if (_runTime.AddSeconds(_span) > dateTime)
                return;
            _runTime = dateTime;
            _thread = new Thread(ExecuteHandler);
            _thread.Start();
        }

        public void Abort()
        {
            _thread.Abort();
        }
        private void ExecuteHandler()
        {
            try
            {
                Execute();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        protected virtual void Execute()
        {

        }
    }
    interface ICronMon
    {
        void AddJob(ICronJob job);
        void Start();
        void Stop();
    }
    class CronMon:ICronMon
    {
        private readonly System.Timers.Timer _timer = new System.Timers.Timer(1000);
        private readonly List<ICronJob> _jobs = new List<ICronJob>();
        private DateTime _last = DateTime.Now;

        public CronMon()
        {
            _timer.AutoReset = true;
           _timer.Elapsed+=_timer_Elapsed;
        }

        private void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (DateTime.Now.Second != _last.Second)
            {
                _last = DateTime.Now;
                foreach (var item in _jobs)
                {
                    item.Run(DateTime.Now);
                }
            }
        }

        public void AddJob(ICronJob job)
        {
            _jobs.Add(job);
        }

        public void Start()
        {
          _timer.Start();
        }

        public void Stop()
        {
            _timer.Stop();
            foreach (var item in _jobs)
            {
                item.Abort();
            }
        }
    }
}