Using C# Task Scheduler Managed Wrapper how to trap an error if task fails and send an email

…衆ロ難τιáo~ 提交于 2019-12-11 12:19:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!