diff --git a/BetterGenshinImpact/GameTask/AutoGeniusInvokation/GeniusInvokationControl.cs b/BetterGenshinImpact/GameTask/AutoGeniusInvokation/GeniusInvokationControl.cs
index 27b096c0..8bbbe80a 100644
--- a/BetterGenshinImpact/GameTask/AutoGeniusInvokation/GeniusInvokationControl.cs
+++ b/BetterGenshinImpact/GameTask/AutoGeniusInvokation/GeniusInvokationControl.cs
@@ -106,9 +106,8 @@ public class GeniusInvokationControl
throw new TaskCanceledException("任务取消");
}
- // The new PausableDelayManager already handles suspend checking and window focus
- // We can just trigger it with a minimal sleep
- TaskControl.CheckAndSleep(0);
+ // Check pause state and window focus without sleeping
+ TaskControl.CheckPauseAndWindowFocus();
if (_ct is { IsCancellationRequested: true })
{
diff --git a/BetterGenshinImpact/GameTask/Common/PausableDelayManager.cs b/BetterGenshinImpact/GameTask/Common/PausableDelayManager.cs
index 189fc740..731166a9 100644
--- a/BetterGenshinImpact/GameTask/Common/PausableDelayManager.cs
+++ b/BetterGenshinImpact/GameTask/Common/PausableDelayManager.cs
@@ -34,14 +34,28 @@ public class PausableDelayManager
SleepWithPauseCheck(millisecondsTimeout);
}
+ ///
+ /// Only checks and handles pause/suspend state and window focus without sleeping.
+ /// Useful for ensuring the game is in the correct state before proceeding.
+ ///
+ public void CheckPauseAndWindowFocus()
+ {
+ CheckAndHandleSuspend();
+ CheckAndActivateGameWindow();
+ }
+
///
/// Performs a pausable sleep with cancellation support.
///
/// Time to sleep in milliseconds
/// Cancellation token
+ /// Thrown when the operation is cancelled
public void Sleep(int millisecondsTimeout, CancellationToken ct)
{
- ct.ThrowIfCancellationRequested();
+ if (ct.IsCancellationRequested)
+ {
+ throw new OperationCanceledException(ct);
+ }
if (millisecondsTimeout <= 0)
{
@@ -55,7 +69,10 @@ public class PausableDelayManager
// Perform the actual sleep with periodic pause checks
SleepWithPauseCheck(millisecondsTimeout, ct);
- ct.ThrowIfCancellationRequested();
+ if (ct.IsCancellationRequested)
+ {
+ throw new OperationCanceledException(ct);
+ }
}
///
@@ -63,9 +80,13 @@ public class PausableDelayManager
///
/// Time to delay in milliseconds
/// Cancellation token
+ /// Thrown when the operation is cancelled
public async Task DelayAsync(int millisecondsTimeout, CancellationToken ct)
{
- ct.ThrowIfCancellationRequested();
+ if (ct.IsCancellationRequested)
+ {
+ throw new OperationCanceledException(ct);
+ }
if (millisecondsTimeout <= 0)
{
@@ -79,7 +100,10 @@ public class PausableDelayManager
// Perform the actual delay with periodic pause checks
await DelayWithPauseCheckAsync(millisecondsTimeout, ct);
- ct.ThrowIfCancellationRequested();
+ if (ct.IsCancellationRequested)
+ {
+ throw new OperationCanceledException(ct);
+ }
}
///
@@ -170,7 +194,10 @@ public class PausableDelayManager
while (remaining > 0)
{
- ct.ThrowIfCancellationRequested();
+ if (ct.IsCancellationRequested)
+ {
+ throw new OperationCanceledException(ct);
+ }
var sleepTime = Math.Min(checkInterval, remaining);
Thread.Sleep(sleepTime);
@@ -195,7 +222,10 @@ public class PausableDelayManager
while (remaining > 0)
{
- ct.ThrowIfCancellationRequested();
+ if (ct.IsCancellationRequested)
+ {
+ throw new OperationCanceledException(ct);
+ }
var delayTime = Math.Min(checkInterval, remaining);
await Task.Delay(delayTime, ct);
diff --git a/BetterGenshinImpact/GameTask/Common/TaskControl.cs b/BetterGenshinImpact/GameTask/Common/TaskControl.cs
index 5b9ebb88..454bddb1 100644
--- a/BetterGenshinImpact/GameTask/Common/TaskControl.cs
+++ b/BetterGenshinImpact/GameTask/Common/TaskControl.cs
@@ -26,6 +26,14 @@ public class TaskControl
_delayManager.Sleep(millisecondsTimeout);
}
+ ///
+ /// Checks and handles pause/suspend state and window focus without sleeping.
+ ///
+ public static void CheckPauseAndWindowFocus()
+ {
+ _delayManager.CheckPauseAndWindowFocus();
+ }
+
public static void Sleep(int millisecondsTimeout)
{
_delayManager.Sleep(millisecondsTimeout);
@@ -128,7 +136,14 @@ public class TaskControl
throw new NormalEndException("取消自动任务");
}
- _delayManager.Sleep(millisecondsTimeout, ct);
+ try
+ {
+ _delayManager.Sleep(millisecondsTimeout, ct);
+ }
+ catch (OperationCanceledException)
+ {
+ throw new NormalEndException("取消自动任务");
+ }
}
public static async Task Delay(int millisecondsTimeout, CancellationToken ct)
@@ -138,7 +153,14 @@ public class TaskControl
throw new NormalEndException("取消自动任务");
}
- await _delayManager.DelayAsync(millisecondsTimeout, ct);
+ try
+ {
+ await _delayManager.DelayAsync(millisecondsTimeout, ct);
+ }
+ catch (OperationCanceledException)
+ {
+ throw new NormalEndException("取消自动任务");
+ }
}
public static Mat CaptureGameImage(IGameCapture? gameCapture)