使用C#任务计划程序托管包装器如果在任务失败并发送电子邮件时如何捕获错误

时间:2015-08-14 21:23:54

标签: c# scheduled-tasks

使用以下code example我创建了一项任务。任务运行并成功发送电子邮件。如果任务失败,我如何以编程方式捕获故障,然后发送电子邮件:

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);
      }
   }

0 个答案:

没有答案