From 8b26dccd22687e31a853f40930e3304d18a0c6cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Sun, 28 Jun 2026 08:33:49 +0800 Subject: [PATCH] =?UTF-8?q?RedoableWorker=20=E7=9A=84=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/General/Progress.cs | 12 ++++++++ Shared/General/Workers/RedoableWorker.cs | 38 ++++++++++++++++++------ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/Shared/General/Progress.cs b/Shared/General/Progress.cs index 036bd77..2c44559 100644 --- a/Shared/General/Progress.cs +++ b/Shared/General/Progress.cs @@ -62,6 +62,18 @@ public class ProgressProvider { public void Finish(ChildrenAction action = ChildrenAction.Finished) => Set(1, skiped: false, action: action); /// 跳过当前项的剩余进度。 public void Skip(ChildrenAction action = ChildrenAction.Skiped) => Set(1, skiped: true, action: action); + /// + /// 重置当前进度,并清除所有子项。 + /// + public void Reset() { + bool changed; + lock (this) { + changed = progressSum > 0; + progressParts = (0, 0, 0); + childrens.Clear(); + } + if (changed) InvokeProgressChanged(); + } // ===================================== 子项进度 ===================================== diff --git a/Shared/General/Workers/RedoableWorker.cs b/Shared/General/Workers/RedoableWorker.cs index da01e16..4420f02 100644 --- a/Shared/General/Workers/RedoableWorker.cs +++ b/Shared/General/Workers/RedoableWorker.cs @@ -21,17 +21,27 @@ public interface IRedoableWorker { } /// -public abstract class RedoableWorkerBase(Func workload) : IRedoableWorker { +public abstract class RedoableWorkerBase(Func workload, ProgressProvider? p = null) : IRedoableWorker { - // ============================================ 状态 ============================================ + // ============================================ 状态与事件 ============================================ /// public bool Running { get { lock (this) return running; } } private bool running; + /// 从空闲状态进入运行状态时触发。 + public event Action? Started; + /// 运行结束并进入空闲状态时触发。 + public event Action? Stopped; /// public bool HasSucceeded { get { lock (this) return hasSucceeded; } } private bool hasSucceeded; + /// 工作负载成功完成时触发,参数为返回值。这可能会在重启前触发。 + public event Action? Succeeded; + /// 工作负载执行失败时触发,参数为发生的异常。这可能会在重启前触发。 + public event Action? Failed; + /// 运行被取消时触发。这可能会在重启前触发。 + public event Action? Canceled; /// public bool LastSucceeded { get { lock (this) return lastSucceeded; } } @@ -73,10 +83,12 @@ public abstract class RedoableWorkerBase(Func wor th.Start(); } private void _Invoke() { + Started?.Invoke(); while (true) { try { realCts!.Token.ThrowIfCancellationRequested(); - TOut result = workload(realCts.Token); // 实际的执行 + p?.Reset(); + TOut result = workload(realCts.Token, p); // 实际的执行 realCts.Token.ThrowIfCancellationRequested(); lock (this) { realCts?.Dispose(); @@ -91,9 +103,14 @@ public abstract class RedoableWorkerBase(Func wor hasSucceeded = true; running = false; idleEvent.Set(); } + Succeeded?.Invoke(result); } catch (Exception ex) { - if (!ex.IsCanceled()) + if (ex.IsCanceled()) { + Canceled?.Invoke(); + } else { + Failed?.Invoke(ex); Logger.Log(ex, $"工作线程执行失败{(pendingRedo ? ",但即将重启,或可忽略" : "")}", pendingRedo ? LogLevel.Info : LogLevel.Warn); + } lock (this) { realCts?.Dispose(); if (pendingRedo) { // 接取重启请求 @@ -106,8 +123,9 @@ public abstract class RedoableWorkerBase(Func wor running = false; idleEvent.Set(); } } - return; + break; } + Stopped?.Invoke(); } /// @@ -126,12 +144,14 @@ public abstract class RedoableWorkerBase(Func wor /// public class RedoableWorker : RedoableWorkerBase { - public RedoableWorker(Func workload) : base(workload) { } - public RedoableWorker(Func workload) : base(_ => workload()) { } + public RedoableWorker(Func workload) : base(workload) { } + public RedoableWorker(Func workload) : base((c, _) => workload(c)) { } + public RedoableWorker(Func workload) : base((_, _) => workload()) { } } /// public class RedoableWorker : RedoableWorkerBase { - public RedoableWorker(Action workload) : base(ct => { workload(ct); return null; }) { } - public RedoableWorker(Action workload) : base(_ => { workload(); return null; }) { } + public RedoableWorker(Func workload) : base(workload) { } + public RedoableWorker(Action workload) : base((c, _) => { workload(c); return null; }) { } + public RedoableWorker(Action workload) : base((_, _) => { workload(); return null; }) { } }