【发布时间】:2020-08-26 23:52:04
【问题描述】:
我正在尝试创建一些电子邮件发送过程。如果没有多线程方法,它可以按预期工作。但是发送所有电子邮件需要更多时间。
在我决定使用多线程之后,我也为多线程进程做了一些异常处理。
这是我的方法,
public enum EmailRequestProcesStatus
{
Pending = 1,
Processing = 2,
Complete = 3
}
public async Task<int> InvokeSendingEmailAsync(string serverFolderPath)
{
// update pending requests
await UpdateEmailRequestToProcessAsync(EmailRequestProcesStatus.Processing);
var invokedDateTime = DateTime.Now;
// first get the CustomerEmail list
var emailList= await GetPendingEmailList();
IList<CustomerRule> customerRules = new List<CustomerRule>();
int ruleLength = 0;
List<Thread> threadList = new List<Thread>();
// email count list
List<int> sendEmailCountList = new List<int>();
foreach (var customer in emailList)
{
ruleLength++;
customerRules.Add(customer);
if (ruleLength % 20 == 0)
{
var custList = customerRules;
Thread t = new Thread(() => ProcessEmailSend(custList, serverFolderPath, invokedDateTime, sendEmailCountList));
threadList.Add(t);
t.Start();
customerRules = new List<CustomerRule>();
}
}
if (customerRules.Count > 0)
{
Thread t = new Thread(() => ProcessEmailSend(customerRules, serverFolderPath, invokedDateTime, sendEmailCountList));
threadList.Add(t);
t.Start();
}
// join the all threads
foreach (var thread in threadList)
{
thread.Join();
}
return sendEmailCountList.Count;
}
private void ProcessEmailSend(IList<CustomerRule> customerList, string serverFolderPath, DateTime invokedDateTime, List<int> sendEmailCountList)
{
foreach(var cust in customerList)
{
var customerDetailList = get customerEmailDetails(cust);
// access some file from file server by giving details
var result = getPdf(customerDetailList.files);
// do the rest of work using
}
}
private filesDto getPdf(filesDto files)
{
try
{
//here I call the file server
}
catch(Exception e)
{
UpdateEmailServiceWhenException();
throw;
}
}
throw 后会被全局异常处理程序捕获。 如果一个工作线程从文件服务器中发现文件未找到的异常,则它作为异常插入到表中并再次抛出。
从此,所有进程都将终止,IIS 应用程序池也将关闭。表中也可能有不明确的多条记录。
如何排序?
【问题讨论】:
-
应用程序池回收、断电,各种事情都可能导致进程突然退出,即使在受控情况下,asp.net 也不知道您的线程。最好在某处(数据库、消息队列等)对电子邮件进行排队,然后让非 asp-net 的东西从该队列中拉取电子邮件请求并可靠地发送电子邮件。
-
@Damien_The_Unbeliever 感谢您的评论。使用队列可以像拥有多个线程一样获得性能提升。如果没有多线程,我不会出现这个错误。
-
你可能会觉得这很有趣:Fire and Forget on ASP.NET
标签: c# multithreading exception iis