Fix exception handling and add CheckPauseAndWindowFocus method

Co-authored-by: huiyadanli <15783049+huiyadanli@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-11-21 02:52:00 +00:00
parent e969b5a96f
commit d30e05b365
3 changed files with 62 additions and 11 deletions

View File

@@ -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 })
{

View File

@@ -34,14 +34,28 @@ public class PausableDelayManager
SleepWithPauseCheck(millisecondsTimeout);
}
/// <summary>
/// Only checks and handles pause/suspend state and window focus without sleeping.
/// Useful for ensuring the game is in the correct state before proceeding.
/// </summary>
public void CheckPauseAndWindowFocus()
{
CheckAndHandleSuspend();
CheckAndActivateGameWindow();
}
/// <summary>
/// Performs a pausable sleep with cancellation support.
/// </summary>
/// <param name="millisecondsTimeout">Time to sleep in milliseconds</param>
/// <param name="ct">Cancellation token</param>
/// <exception cref="OperationCanceledException">Thrown when the operation is cancelled</exception>
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);
}
}
/// <summary>
@@ -63,9 +80,13 @@ public class PausableDelayManager
/// </summary>
/// <param name="millisecondsTimeout">Time to delay in milliseconds</param>
/// <param name="ct">Cancellation token</param>
/// <exception cref="OperationCanceledException">Thrown when the operation is cancelled</exception>
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);
}
}
/// <summary>
@@ -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);

View File

@@ -26,6 +26,14 @@ public class TaskControl
_delayManager.Sleep(millisecondsTimeout);
}
/// <summary>
/// Checks and handles pause/suspend state and window focus without sleeping.
/// </summary>
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)