/// /// 异步工具类 /// public class TaskTools { /// /// 是否 在执行回调函数之前修改Running状态 /// public bool ChangeRunningStateBeforeCallback { get; private set; } /// /// 是否 正在执行异步任务 /// public bool Running { get; private set; } public TaskTools() : this(false) { } /// /// /// /// 是否 在执行回调函数之前修改Running状态 默认值false public TaskTools(bool changeRunningStateBeforeCallback) { this.ChangeRunningStateBeforeCallback = changeRunningStateBeforeCallback; } /// /// 执行异步任务 /// /// 异步任务返回值类型 /// 操作UI时需要Invoke的控件 /// 将要执行的任务任务 /// 异步任务执行完毕后执行的回调函数 public void Run (Control control, Func asyncFunc, Action callback) { if (this.Running) throw new InvalidOperationException(" the task is running "); try { this.Running = true; Task task = new Task (() => { try { return asyncFunc(); } catch (Exception ex) { Console.WriteLine(ex.Message); return default(T); } }); task.Start(); TaskContinue (control, task, callback); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { this.Running = false; } } /// /// 执行异步任务 /// /// 异步任务返回值类型 /// 操作UI时需要Invoke的控件 /// 异步任务的传入参数 /// 将要执行的任务任务 /// 异步任务执行完毕后执行的回调函数 public void Run (Control control, object args, Func