itsfatduck/optimizerDuck · error · InvalidOperationException

result.Error ?? "Enable failed"

Error message

result.Error ?? "Enable failed"

What it means

Generic guard exception in ScheduledTasksViewModel.ToggleTask: ScheduledTaskService.EnableTask returned Ok=false, so the raw OpResult error text (or the 'Enable failed' fallback when the provider gave no detail) is rethrown as InvalidOperationException inside Task.Run to land in the VM's catch block and surface as a UI error. The input at fault is task.FullPath — typically the task was deleted or renamed in Task Scheduler since the list was loaded, or access was denied.

Solutions

  1. Refresh the task list — the task at FullPath may have been deleted or renamed outside the app since the snapshot was taken
  2. Run the app elevated (requireAdministrator manifest) so enabling tasks under protected folders (e.g. \Microsoft\...) succeeds
  3. Verify the task still exists via Get-ScheduledTask and re-enable manually (Enable-ScheduledTask) to see the real OS-level error
  4. If result.Error is null, check the Serilog output for the underlying ScheduledTaskService failure detail
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at optimizerDuck/UI/ViewModels/Pages/ScheduledTasksViewModel.cs:105 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of itsfatduck/optimizerDuck@36acf585ae (2026-09-13). Data as JSON: /api/errors/68f36310af561f08. Report an issue: GitHub.

Appendix: source

Thrown at optimizerDuck/UI/ViewModels/Pages/ScheduledTasksViewModel.cs:105

            _logger.LogError(ex, "Failed to open Task Scheduler");
        }
    }

    [RelayCommand]
    private async Task ToggleTask(ScheduledTaskModel? task)
    {
        if (task == null)
            return;
        try
        {
            await Task.Run(() =>
            {
                var call = UiCall();
                if (task.IsEnabled)
                {
                    var result = ScheduledTaskService.EnableTask(call, task.FullPath);
                    if (!result.Ok)
                        throw new InvalidOperationException(result.Error ?? "Enable failed");
                    _logger.LogInformation(
                        "Enabled task {Name} ({Path})",
                        task.Name,
                        task.FullPath
                    );
                }
                else
                {
                    var result = ScheduledTaskService.DisableTask(call, task.FullPath);
                    if (!result.Ok)
                        throw new InvalidOperationException(result.Error ?? "Disable failed");
                    _logger.LogInformation(
                        "Disabled task {Name} ({Path})",
                        task.Name,
                        task.FullPath
                    );
                }
            });

View on GitHub (pinned to 36acf585ae)