/// <summary> /// 初始化一个任务 /// </summary> /// <param name="cfgFile">任务配置文件路径,如果已经初始化,则返回任务ID</param> /// <returns>返回任务对象</returns> public static Task InitTask(string cfgFile) { var fullPath = Snail.IO.PathUnity.GetFullPath(cfgFile); if (string.IsNullOrEmpty(fullPath)) { return(null); } Task task = null; lock (LockObj) { if (!BufferTasks.ContainsKey(fullPath)) { task = new Task(fullPath); task.OnStart += (sender, e) => { OnTaskRunning?.Invoke(task, new TaskEventArgs() { Task = task }); }; task.OnStop += (sender, e) => { OnTaskComplete?.Invoke(task, new TaskEventArgs() { Task = task }); }; BufferTasks.Add(fullPath, task); } } return(BufferTasks[fullPath]); }
/// <summary> /// 初始化一个任务 /// </summary> /// <param name="cfgFile">任务配置文件路径,如果已经初始化,则返回任务ID</param> /// <returns>返回任务对象</returns> public static Task InitTask(string cfgFile) { var fullPath = SnailCore.IO.PathUnity.GetFullPath(cfgFile); if (string.IsNullOrEmpty(fullPath)) { throw new Exception("could not find the file with path:" + cfgFile); } Task task = null; lock (LockObj) { if (!BufferTasks.ContainsKey(fullPath)) { task = new Task(fullPath); foreach (var kv in BufferTasks) { if (kv.Value.TaskId == task.TaskId) { throw new Exception(string.Format("任务id:{0},重复出现,请检查配置,并重新启动任务.", task.TaskId)); } } task.OnStart += (sender, e) => { try { OnTaskRunning?.Invoke(sender, e); } catch (Exception ex) { LoggerProxy.Error(LogSource, "invoke OnTaskRunning failed.", ex); } }; task.OnStop += (sender, e) => { try { OnTaskComplete?.Invoke(sender, e); } catch (Exception ex) { LoggerProxy.Error(LogSource, "invoke OnTaskComplete failed.", ex); } }; task.OnError += (sender, e) => { try { OnTaskError?.Invoke(sender, e); } catch (Exception ex) { LoggerProxy.Error(LogSource, "invoke OnTaskError failed.", ex); } }; BufferTasks.Add(fullPath, task); } } return(BufferTasks[fullPath]); }