{"record":{"id":"fccad87fb6f53bfe","repo":"itsfatduck/optimizerDuck","slug":"scheduledtasks-error-tasknotfound-localized-via-loc-instance","errorCode":null,"errorMessage":"ScheduledTasks.Error.TaskNotFound (localized via Loc.Instance, formatted with fullPath)","messagePattern":"ScheduledTasks\\.Error\\.TaskNotFound \\(localized via Loc\\.Instance, formatted with fullPath\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"optimizerDuck/Services/Optimization/Providers/ScheduledTaskService.cs","lineNumber":62,"sourceCode":"\n    /// <summary>Disables a scheduled task, recording the change into <paramref name=\"call\"/>.</summary>\n    /// <param name=\"call\">The explicit call context: change collector, logger and cancellation token.</param>\n    /// <param name=\"fullPath\">The full path of the task to disable.</param>\n    /// <returns>The outcome of the disable request.</returns>\n    public static OpResult DisableTask(OpCall call, string fullPath)\n    {\n        ArgumentNullException.ThrowIfNull(call);\n\n        var description = ServiceStrings.Format(\n            ServiceStrings.ScheduledTaskDescriptionDisable,\n            fullPath\n        );\n        try\n        {\n            using var ts = new TaskService();\n            var task =\n                ts.GetTask(fullPath)\n                ?? throw new InvalidOperationException(\n                    Loc.Instance[\"ScheduledTasks.Error.TaskNotFound\", fullPath]\n                );\n\n            var wasEnabled = task.Enabled;\n            task.Enabled = false;\n\n            // Record revert step: restore to previous enabled state\n            ScheduledTaskRevertStep? revertStep = null;\n            if (wasEnabled)\n                revertStep = new ScheduledTaskRevertStep\n                {\n                    FullPath = fullPath,\n                    OriginalEnabled = true,\n                };\n\n            call.Logger.LogInformation(\"Disabled task {Path}\", fullPath);\n            call.Changes.Add(ServiceStrings.ScheduledTaskName, description, true, revertStep);\n            return OpResult.Success(revertStep);","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/itsfatduck/optimizerDuck/blob/36acf585ae122a09f0e29eb4acedfaf94e81c0de/optimizerDuck/Services/Optimization/Providers/ScheduledTaskService.cs#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nScheduledTaskService.DisableTask(\"\\Vendor\\UpdateTask\");\n// after\nvar state = ScheduledTaskService.GetTaskEnabledState(\"\\\\Vendor\\\\UpdateTask\", logger);\nif (state.State is not TaskEnabledState.NotFound)\n    ScheduledTaskService.DisableTask(\"\\\\Vendor\\\\UpdateTask\");","handlingStrategy":"validation","validationCode":"var state = ScheduledTaskService.GetTaskEnabledState(fullPath, logger);\nif (state.State == TaskEnabledState.NotFound) return; // skip — nothing to disable","typeGuard":null,"tryCatchPattern":"try { ScheduledTaskService.DisableTask(fullPath); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"TaskNotFound\"))\n{ logger.LogWarning(\"Task {Path} already gone; skipping\", fullPath); }","preventionTips":["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)"],"tags":["csharp","scheduled-tasks","windows","task-not-found"],"backgroundTag":"entity-not-found","analyzedSha":"36acf585ae122a09f0e29eb4acedfaf94e81c0de","analyzedAt":"2026-09-13T08:30:13.845Z","contentChangedAt":"2026-09-13T08:30:13.845Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}