itsfatduck/optimizerDuck · error · InvalidOperationException
ScheduledTasks.Error.TaskNotFound (localized via…
Error message
ScheduledTasks.Error.TaskNotFound (localized via Loc.Instance, formatted with fullPath)
What it means
ScheduledTaskService.DisableTask throws InvalidOperationException with the localized 'ScheduledTasks.Error.TaskNotFound' message (formatted with fullPath) when ts.GetTask(fullPath) returns null — the task does not exist in Task Scheduler at the given path. The service treats disabling a nonexistent task as a caller error rather than a no-op.
Solutions
- Confirm the path with: schtasks /query /fo LIST | findstr /i <TaskName>, or Get-ScheduledTask -TaskPath.
- Check the task exists before disabling (GetTaskEnabledState(fullPath) returns NotFound) and skip instead of calling DisableTask.
- Update the optimization's task path for the current Windows/vendor version.
- List tasks via ScheduledTaskService.GetAllTasks() to get the exact FullPath at runtime instead of hardcoding.
Example fix
// before
ScheduledTaskService.DisableTask("\Vendor\UpdateTask");
// after
var state = ScheduledTaskService.GetTaskEnabledState("\\Vendor\\UpdateTask", logger);
if (state.State is not TaskEnabledState.NotFound)
ScheduledTaskService.DisableTask("\\Vendor\\UpdateTask"); Defensive patterns
Strategy: validation
Validate before calling
var state = ScheduledTaskService.GetTaskEnabledState(fullPath, logger); if (state.State == TaskEnabledState.NotFound) return; // skip — nothing to disable
Try / catch
try { ScheduledTaskService.DisableTask(fullPath); }
catch (InvalidOperationException ex) when (ex.Message.Contains("TaskNotFound"))
{ logger.LogWarning("Task {Path} already gone; skipping", fullPath); } Prevention
- Resolve FullPath at runtime via GetAllTasks instead of hardcoding
- Confirm task existence before disabling (GetTaskEnabledState)
- Keep bloatware task lists updated per Windows/vendor version
- Account for per-account task folders (different user context)
When it happens
Trigger: Calling DisableTask with a wrong or stale full path (missing leading folder, renamed task), a task deleted by Windows/vendor between listing and disabling, or referencing a task that exists only under a different account's task folder.
Common situations: Bloatware list out of date — vendor removed the task in a new version; hardcoded task path with wrong casing/folder; task removed by a previous optimizer run; localized task folder names differing on non-English Windows.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- Scheduled task verify failed at
- Scheduled task verify failed at
- Failed to create registry key
- result.Error ?? Description
- Registry verify failed at
AI-assisted analysis of itsfatduck/optimizerDuck@36acf585ae (2026-09-13).
Data as JSON: /api/errors/fccad87fb6f53bfe.
Report an issue: GitHub.
Appendix: source
Thrown at optimizerDuck/Services/Optimization/Providers/ScheduledTaskService.cs:62
/// <summary>Disables a scheduled task, recording the change into <paramref name="call"/>.</summary>
/// <param name="call">The explicit call context: change collector, logger and cancellation token.</param>
/// <param name="fullPath">The full path of the task to disable.</param>
/// <returns>The outcome of the disable request.</returns>
public static OpResult DisableTask(OpCall call, string fullPath)
{
ArgumentNullException.ThrowIfNull(call);
var description = ServiceStrings.Format(
ServiceStrings.ScheduledTaskDescriptionDisable,
fullPath
);
try
{
using var ts = new TaskService();
var task =
ts.GetTask(fullPath)
?? throw new InvalidOperationException(
Loc.Instance["ScheduledTasks.Error.TaskNotFound", fullPath]
);
var wasEnabled = task.Enabled;
task.Enabled = false;
// Record revert step: restore to previous enabled state
ScheduledTaskRevertStep? revertStep = null;
if (wasEnabled)
revertStep = new ScheduledTaskRevertStep
{
FullPath = fullPath,
OriginalEnabled = true,
};
call.Logger.LogInformation("Disabled task {Path}", fullPath);
call.Changes.Add(ServiceStrings.ScheduledTaskName, description, true, revertStep);
return OpResult.Success(revertStep);View on GitHub (pinned to 36acf585ae)