Пример #1
0
 public static void Add(string taskName, TaskObject obj)
 {
     lock (lockObj)
     {
         if (!dictTask.ContainsKey(taskName))
         {
             dictTask.Add(taskName, obj);
         }
     }
 }
Пример #2
0
        private static void Execute(KeyValuePair <string, TaskObject> keyObject)
        {
            string     taskName     = keyObject.Key;
            TaskObject taskObj      = keyObject.Value;
            var        allowExecute = false;

            if (!taskObj.IsExecuted)
            {
                var time = (DateTime.Now - taskObj.LastExecuteTime).TotalSeconds;
                if (time > taskObj.ExecuteInterval)
                {
                    allowExecute = true;
                }
            }
            //可以运行
            if (allowExecute)
            {
                lock (lockObj)
                {
                    taskObj.SetState(true);
                }
                var ia = taskObj.Action.BeginInvoke(callback =>
                {
                    while (!callback.IsCompleted)
                    {
                        callback.AsyncWaitHandle.WaitOne(100);
                    }
                }, null);
                taskObj.Action.EndInvoke(ia);
                lock (lockObj)
                {
                    dictTask[taskName].LastExecuteTime = DateTime.Now;
                    taskObj.SetState(false);
                }
            }
        }