问题
Using following code example I created a task. The task runs and sends an email afterwards successfully. If the task fails how do I programmatically trap the failure and then sends an email:
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Does something";
// Add a trigger that, starting tomorrow, will fire every other week on Monday
// and Saturday and repeat every 10 minutes for the following 11 hours
WeeklyTrigger wt = new WeeklyTrigger();
wt.StartBoundary = DateTime.Today.AddDays(1);
wt.DaysOfWeek = DaysOfTheWeek.Monday | DaysOfTheWeek.Saturday;
wt.WeeksInterval = 2;
wt.Repetition.Duration = TimeSpan.FromHours(11);
wt.Repetition.Interval = TimeSpan.FromMinutes(10);
td.Triggers.Add(wt)
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));
td.Actions.Add(new EmailAction("Test from win schedular", "myemaild@mydomain.com", "youremail@yourdomain.com", "Test body content", "smtp.test.com"));
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition("Test", td);
}
}
来源:https://stackoverflow.com/questions/32018859/using-c-sharp-task-scheduler-managed-wrapper-how-to-trap-an-error-if-task-fails