babalae/better-genshin-impact · warning · InvalidOperationException
未处于录制中状态,无法停止
Error message
未处于录制中状态,无法停止
What it means
StopRecord requires the recorder to be in the Recording state. If Status is anything else (Start in progress, or Stop), it throws InvalidOperationException because there is no active session to finalize and serializing now would lose data or produce garbage.
Source
Thrown at BetterGenshinImpact/Core/Recorder/GlobalKeyMouseRecord.cs:118
if (taskTriggerStopped)
{
TaskTriggerDispatcher.Instance().StartTimer();
}
Status = KeyMouseRecorderStatus.Stop;
_logger.LogError(ex, "启动键鼠录制失败,相对鼠标捕获方式:{InputType}", inputType);
await ThemedMessageBox.ErrorAsync(
$"启动键鼠录制失败:{ex.Message}",
"键鼠录制启动失败");
return false;
}
}
public string StopRecord()
{
if (Status != KeyMouseRecorderStatus.Recording)
{
throw new InvalidOperationException("未处于录制中状态,无法停止");
}
_timer.Stop();
var recorder = _recorder;
_recorder = null;
var relativeMouseSubscription = _relativeMouseSubscription;
_relativeMouseSubscription = null;
try
{
relativeMouseSubscription?.Dispose();
return recorder?.ToJsonMacro() ?? string.Empty;
}
finally
{
_logger.LogInformation("录制:{Text}", "结束录制");
TaskTriggerDispatcher.Instance().StartTimer();View on GitHub (pinned to a7cb36712d)
Solutions
- Check Status == KeyMouseRecorderStatus.Recording before calling StopRecord.
- Guard the stop hotkey handler to ignore presses when not recording.
- Disable the stop button/UI when Status is not Recording.
- Ensure callers react to a failed StartRecord (returns false) instead of proceeding to StopRecord.
Example fix
// before
public string StopRecord()
{
if (Status != KeyMouseRecorderStatus.Recording)
throw new InvalidOperationException("未处于录制中状态,无法停止");
// ...
}
// after: no-op when idle
public string StopRecord()
{
if (Status != KeyMouseRecorderStatus.Recording)
{
_logger.LogWarning("StopRecord ignored: current status is {Status}", Status);
return string.Empty;
}
// ...
} Defensive patterns
Strategy: validation
Validate before calling
if (GlobalKeyMouseRecord.Instance.Status != KeyMouseRecorderStatus.Recording)
{
// not recording; ignore or log instead of stopping
return string.Empty;
}
return GlobalKeyMouseRecord.Instance.StopRecord(); Type guard
static bool IsRecording => GlobalKeyMouseRecord.Instance.Status == KeyMouseRecorderStatus.Recording;
Try / catch
try { return GlobalKeyMouseRecord.Instance.StopRecord(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("未处于录制中状态"))
{ _logger.LogWarning(ex, "StopRecord called while not recording."); return string.Empty; } Prevention
- Gate the stop hotkey and stop UI button on Status == Recording.
- Treat a failed StartRecord (returns false) as 'not recording' before allowing stop.
- Make StopRecord idempotent by returning early when idle instead of throwing.
When it happens
Trigger: The stop hotkey fires when no recording is active; StopRecord is called twice in quick succession; the stop handler runs after StartRecord failed and reset status to Stop.
Common situations: Hotkey double-fire; stop UI button enabled while not recording; race between StartRecord's countdown and a stop press; caller ignored that StartRecord returned false.
Related errors
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/c1ae6816cacf7f6b.
Report an issue: GitHub.