using System.Collections.Concurrent; namespace YLErp.Helpers { //public enum ProgressName //{ // ClientBalanceReport //} public static class ProgressHelper { static ConcurrentDictionary> goingTasks = new ConcurrentDictionary>(); private static readonly object _asyncLock = new object(); public static bool Start(IEnumerable inputList, string key, Func callback) { lock (_asyncLock) { if (goingTasks.TryGetValue(key, out var ret) && ret.status == ProgressBarStatus.IDLE) { goingTasks.TryRemove(key, out _); } if (goingTasks.TryAdd(key, new ProgressInfo(inputList, callback))) { return true; } return false; } } public static bool Start(IEnumerable inputList, string key, Func callback, object extendParams) { lock (_asyncLock) { if (goingTasks.TryGetValue(key, out var ret) && ret.status == ProgressBarStatus.IDLE) { goingTasks.TryRemove(key, out _); } if (goingTasks.TryAdd(key, new ProgressInfo(inputList, callback, extendParams))) { return true; } return false; } } public static bool Start(IEnumerable inputList, string key, Func callback, Func finalFunc, object extendParams) { lock (_asyncLock) { if (goingTasks.TryGetValue(key, out var ret) && ret.status == ProgressBarStatus.IDLE) { goingTasks.TryRemove(key, out _); } if (goingTasks.TryAdd(key, new ProgressInfo(inputList, callback, finalFunc, extendParams))) { return true; } return false; } } public static bool Start(IEnumerable inputList, string key, Func, string> callback, Func finalFunc, object extendParams) { lock (_asyncLock) { if (goingTasks.TryAdd(key, new ProgressInfo(inputList, callback, finalFunc, extendParams))) { return true; } return false; } } public static ProgressInfo ObservateProcess(string key) { if (goingTasks.ContainsKey(key)) { var ret = goingTasks[key]; if (ret.status == ProgressBarStatus.IDLE) { goingTasks.TryRemove(key, out ret); } return ret; } else { return null; } } public static bool Stop(string key) { if (goingTasks.ContainsKey(key)) { var ret = goingTasks[key]; if (ret.status == ProgressBarStatus.IDLE) { } else { ret.status = ProgressBarStatus.STOPPING; } goingTasks.TryRemove(key, out _); return true; } else { return false; } } } public class ProgressInfo { public int total { get; private set; } public int done { get; private set; } public List message { get; private set; } public ProgressBarStatus status { get; set; } public bool ignoreIndex { get; set; } /// /// 保存 finalFunc 的返回值,传到前端; /// public object resultObj { get; private set; } public ProgressInfo(IEnumerable goings, Func del) { this.total = goings.Count(); this.done = 0; this.status = ProgressBarStatus.RUNNING; message = new List(); Thread th = new Thread(() => DoProgress(goings, del)); th.Start(); } public ProgressInfo(IEnumerable goings, Func del, object extendParams) { this.total = goings.Count(); this.done = 0; this.status = ProgressBarStatus.RUNNING; message = new List(); Thread th = new Thread(() => DoProgress(goings, del, extendParams)); th.Start(); } public ProgressInfo(IEnumerable goings, Func del, Func final, object extendParams) { this.total = goings.Count(); this.done = 0; this.status = ProgressBarStatus.RUNNING; message = new List(); Thread th = new Thread(() => DoProgress(goings, del, final, extendParams)); th.Start(); } public ProgressInfo(IEnumerable goings, Func, string> del, Func final, object extendParams) { this.total = goings.Count(); this.done = 0; this.status = ProgressBarStatus.RUNNING; message = new List(); Thread th = new Thread(() => DoProgress(goings, del, final, extendParams)); th.Start(); } void DoProgress(IEnumerable input, Func callback) { try { foreach (var item in input) { if (this.status != ProgressBarStatus.RUNNING) { break; } try { this.message.Add(callback(item)); } catch (Exception ex) { LogFactory.GetLogger("ProgressHelper").Error(ex); } if (this.done < this.total) { this.done++; } } } catch (Exception ex) { LogFactory.GetLogger("ProgressHelper").Error("进度无法继续:" + ex.Message); } finally { this.status = ProgressBarStatus.IDLE; } } void DoProgress(IEnumerable input, Func callback, object extendParams) { try { foreach (var item in input) { if (this.status != ProgressBarStatus.RUNNING) { break; } try { this.message.Add(callback(item, extendParams)); } catch (Exception ex) { LogFactory.GetLogger("ProgressHelper").Error(ex); } if (this.done < this.total) { this.done++; } } } catch (Exception ex) { LogFactory.GetLogger("ProgressHelper").Error("进度无法继续:" + ex.Message); } finally { this.status = ProgressBarStatus.IDLE; } } void DoProgress(IEnumerable input, Func callback, Func finalFunc, object extendParams) { try { foreach (var item in input) { if (this.status != ProgressBarStatus.RUNNING) { break; } try { this.message.Add(callback(item, extendParams)); } catch (Exception ex) { LogFactory.GetLogger("ProgressHelper").Error(ex); } if (this.done < this.total) { this.done++; } } } catch (Exception ex) { LogFactory.GetLogger("ProgressHelper").Error("进度无法继续:" + ex.Message); } finally { try { if (this.status == ProgressBarStatus.RUNNING) { resultObj = finalFunc(extendParams); } } catch (Exception e) { LogFactory.GetLogger("ProgressHelper").Error("进度完成时出错:" + e.Message); } finally { this.status = ProgressBarStatus.IDLE; } } } void DoProgress(IEnumerable input, Func, string> callback, Func finalFunc, object extendParams) { try { foreach (var item in input) { if (this.status != ProgressBarStatus.RUNNING) { break; } try { this.message.Add(callback(item, extendParams, this)); } catch (Exception ex) { LogFactory.GetLogger("ProgressHelper").Error(ex); } if (this.ignoreIndex) { this.message.Add(callback(item, extendParams, this)); } if (this.done < this.total) { this.done++; } } } catch (Exception ex) { LogFactory.GetLogger("ProgressHelper").Error("进度无法继续:" + ex.Message); } finally { try { if (this.status == ProgressBarStatus.RUNNING) { resultObj = finalFunc(extendParams); } } catch (Exception e) { LogFactory.GetLogger("ProgressHelper").Error("进度完成时出错:" + e.Message); } finally { this.status = ProgressBarStatus.IDLE; } } } } public enum ProgressBarStatus { /// /// 空闲 /// IDLE, /// /// 运行中 /// RUNNING, /// /// 停止中 /// STOPPING, } }