【发布时间】:2019-01-17 00:19:59
【问题描述】:
当我抛出异常时,我的函数将其冒泡并在任务异常中捕获它,而不是继续它会破坏代码 - 下面是我的代码
public override void Run()
{
SendRenewalsEmail("ddd@xxx.com", " Email Body from test More", "Test Email from Service another Test");
}
private async void SendRenewalsEmail(string userEmail, string emailBody, string emailSubject)
{
string replyFromEmailAddress = "renewals@xxx.net";
string cc = "";
string bcc = "ccc@xxxx.com";
SMTPMailHelperAsync sMTPMailHelperAsync = new SMTPMailHelperAsync();
var x= await sMTPMailHelperAsync.SendEmailAsync(userEmail, cc, bcc, emailSubject, SMTPMailHelperAsync.ProcessTemplate(emailBody, "Renewals.html", emailSubject), replyFromEmailAddress);
if (x.MailSent)
{
throw new Exception("after mail Test more service");
}
}
以及它被捕获的任务
var task= Task<PluginInstance>.Run<PluginInstance>(() => {
thisPlugin.LastRunStart = DateTime.Now.ToLocalTime();
try
{
thisPlugin.Plugin.Run();
thisPlugin.LastRunStatus = Enums.RunStatus.Success;
thisPlugin.LastRunMessage = "";
}
catch (Exception ex)
{
thisPlugin.LastRunStatus = Enums.RunStatus.Failed;
thisPlugin.LastRunMessage = ex.Message;
}
thisPlugin.LastRunEnd = DateTime.Now.ToLocalTime();
return thisPlugin;
});
ListOfTask.Add(task);
【问题讨论】:
标签: c#-4.0 task-parallel-library