c#-4.0 - “应用程序处于中断模式” C# 使用 throw 冒泡异常时
问题描述
当我抛出异常时,我的函数将其冒泡并在任务异常中捕获它,而不是继续它会破坏代码-下面是我的代码
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);
解决方案
您不得使用async void
. 这是一种特殊情况,仅为事件处理程序保留。您的async
方法必须返回Task
:
private async Task SendRenewalsEmail(…)
然后,你的Plugin.Run
方法就坏了。它也应该如此async
。
一旦你开始async - await
,你就做到了。
推荐阅读
- javascript - 在循环中构建多个 SlimSelect 对象 - 只有最后一个对象有效
- python - Python 中的时间复杂度 str.isdigit() 是什么?
- python - 如何将此元组列表更改为以下格式?
- aframe - a-frame a-video 不工作黑色或没有结果
- animation - 从实际大小而不是 0 SwiftUI 缩放形状
- node.js - rrule:不支持在没有 Luxon 的情况下使用 TZID
- html - Grid 项目导航栏中的 Flexbox 下拉菜单
- python - 这个 shell 脚本行是做什么的?
- python - Pandas - 排除数字列为 NaN 的行
- tensorflow - logits 和标签必须具有相同的形状 ((None, 4, 1, 2) vs (None, 2))