Exemple #1
0
 /// <summary>
 /// 注册定时任务
 /// </summary>
 /// <param name="key"></param>
 /// <param name="handle"></param>
 public static void Register(TimingTasksAttribute key, Delegate handle)
 {
     if (!_timingTasks.ContainsKey(key))
     {
         _timingTasks[key]     = handle;
         _timingTaskInfos[key] = new TimingTaskInfo()
         {
             Task = key, Key = Guid.NewGuid().ToString()
         };
     }
 }
Exemple #2
0
 /// <summary>
 /// 执行任务
 /// </summary>
 /// <param name="key"></param>
 public static void ExecuteTask(TimingTasksAttribute key)
 {
     if (_timingTasks.ContainsKey(key))
     {
         _timingTasks[key].DynamicInvoke(null);
         //记录任务运行信息
         if (_timingTaskInfos[key].StartExecuteTime == null)
         {
             _timingTaskInfos[key].StartExecuteTime = DateTime.Now;
         }
         _timingTaskInfos[key].ExecuteCount += 1;
     }
 }
        /// <summary>
        /// 绑定任务
        /// </summary>
        /// <param name="type"></param>
        private static void BindTask(Type type)
        {
            var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);

            foreach (var m in methods)
            {
                TimingTasksAttribute attribute = m.GetCustomAttributes(typeof(TimingTasksAttribute), true)?.FirstOrDefault() as TimingTasksAttribute;
                if (attribute != null)
                {
                    Dictionary <string, Type> paras = new Dictionary <string, Type>();
                    attribute.TaskAddress = string.Format("{0} 【{1}.{2}】", type.Assembly.Location, type.FullName, m.Name);
                    var instance = Activator.CreateInstance(type);
                    var resType  = m.ReturnType;
                    paras.Add("returns", resType);

                    if (string.IsNullOrEmpty(attribute.Name))
                    {
                        attribute.Name = string.Format("{0}.{1}", type.FullName, m.Name);
                    }

                    TimingTasksHandler.Register(attribute, Delegate.CreateDelegate(System.Linq.Expressions.Expression.GetDelegateType(paras.Values.ToArray()), instance, m));
                }
            }
        }
Exemple #4
0
 /// <summary>
 /// 卸载定时任务
 /// </summary>
 /// <param name="key"></param>
 public static void UnRegister(TimingTasksAttribute key)
 {
     _timingTasks.Remove(key);
     _timingTaskInfos.Remove(key);
 }