• C#封装定时执行任务类


    a.日常开发中经常会遇到定时去执行一些操作,比如定时更新数据。A类需要做我们写个Timer定时去取数据,这时候B类,C类也需要做这样的事情,是不是需要写三次重复代码?

    这时候把timer封装成一个帮助的类来解决这个问题。

    b.封装的思路:

       1.这个类具有定时处理的功能

       2.既然定时处理,处理什么事情呢?需要开放一个委托,让使用者告诉这个类需要做什么事情。

       3.那多久做一次这件事情呢?需要开放一个参数,时间间隔。

       4.封装的意义就是多处使用,且调用者不用关心内部实现。简单来说就是让使用者调用起来方便。

    c.既然这个帮助类是为整个解决方案服务的,这时候起名字就定为。TimerService。

    下面上个效果图,直观的了解定时任务。

     1    public class TimerService
     2     {
     3         #region Private Members
     4         private static TimerService _instance = new TimerService();
     5         #endregion
     6 
     7         #region Constructors
     8         private TimerService()
     9         {
    10         }
    11         #endregion
    12 
    13         #region Public Properties
    14         public static TimerService Instance
    15         {
    16             get { return _instance; }
    17         }
    18         #endregion
    19 
    20         #region Public Methods 
    21         public void Schedule(Action action, int interval)
    22         {
    23              TimerTask task = default(TimerTask);
    24              task = new TimerTask(action, interval);
    25         }
    26         #endregion
    27 
    28         #region Private Methods
    29         #endregion 
    30     }
    TimerService
     1  class TimerTask
     2     {
     3         #region Private Members
     4         public string _desc;
     5         public System.Timers.Timer _timer;
     6         private WeakReference<object> _target;
     7         private MethodInfo _method;
     8         private GCHandle _gcHandle;
     9         #endregion
    10 
    11         #region Constructors
    12         public TimerTask(Action ac, int interval)
    13         {
    14             _gcHandle = GCHandle.Alloc(this);
    15             _method = ac.Method;
    16             _target = new WeakReference<object>(ac.Target);
    17             _desc = ac.Method + "@" + ac.Target;
    18             _timer = new System.Timers.Timer(interval * 1000);
    19             _timer.Elapsed += Timer_Elapsed;
    20             _timer.Start();
    21         }
    22         #endregion
    23 
    24         #region Public Properties
    25         #endregion
    26 
    27         #region Public Methods
    28         public void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    29         {
    30             try
    31             {
    32                 object t;
    33                 bool hasRef = _target.TryGetTarget(out t);
    34                 if (hasRef)
    35                 {
    36                     _method.Invoke(t, null);
    37                 }
    38                 else
    39                 {
    40                     _timer.Stop();
    41                     _gcHandle.Free();
    42                     Console.WriteLine("Auto free timer: " + _desc);
    43                 }
    44                 t = null;
    45             }
    46             catch (Exception ex)
    47             {
    48                 Console.WriteLine(ex.Message);
    49                 Console.WriteLine(ex.StackTrace);
    50             }
    51         }
    52         #endregion
    53 
    54         #region Private Methods
    55         #endregion 
    56     }
    TimerTask

    如有任何错误欢迎指出。

  • 相关阅读:
    make 实例 一 3463
    python3 中对arrow库的总结(转发)
    impala 导出CSV 或excel
    设置虚拟机IP
    centos7 tomcat9
    eclipse 创建普通maven项目
    java log4j日志配置
    java运行jar命令提示没有主清单属性
    Java 读取 .properties 配置文件
    python 机器学习多项式回归
  • 原文地址:https://www.cnblogs.com/justzhuzhu/p/7307991.html
Copyright © 2020-2023  润新知