Search code examples
c#scheduled-taskseventtriggerwindows-task-schedulerevent-viewer

Task Scheduler - When the event is added to the event log the created task not running


Here is the code that I used to create task and bind it to event log, after doing some action I can see in event logs that new event log was created, but task not stared, what was done incorrect here ?

public void CreateTaskSchedulerTask(Trigger trigger, string name,string description, string path, string arguments, string workingDirectory = null)//"Remove unnecessary dependencies"
            {
                TaskDefinition td = TaskService.Instance.NewTask();
                td.Triggers.Add(trigger);
                //td.Principal.LogonType = TaskLogonType.InteractiveToken;
                td.RegistrationInfo.Description = description;
                td.Actions.Add(new ExecAction(path/*"notepad.exe"*/, arguments/*"c:\\test.log"*/, workingDirectory));
                td.Settings.Enabled = true;
                td.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal;
                td.Settings.StartWhenAvailable = true;
                td.Settings.DisallowStartIfOnBatteries = false;
                td.Settings.StopIfGoingOnBatteries = false;
               // td.Settings.RunOnlyIfLoggedOn = false;
    
                TaskService.Instance.RootFolder.RegisterTaskDefinition(name, td);
            }
    
            public EventTrigger CreateEventTrigger(string log, string source, int? eventId)//"Microsoft-Windows-AppXDeploymentServer/Operational", "AppXDeployment-Server", 400
            {
                return new EventTrigger(log, source, eventId);
            }

enter image description here

enter image description here


Solution

  • "AppXDeployment-Server" should be "Microsoft-Windows-AppXDeployment-Server" windows event viewer for some reason it doesn't show the full name.

    enter image description here