找出哪个Parallel ForEach线程抛出了异常

本文关键字:异常 线程 Parallel ForEach | 更新日期: 2023-09-27 18:12:03

大家好,提前感谢您的建议,

我有一个Parallel For-each循环,它发送大量电子邮件。我想知道的是如何找出哪个线程抛出了SmtpException。我想知道不能发送到哪个邮箱

protected void btnSend_Click(object sender, EventArgs e)
{
    try
    {
        if (Page.IsValid)
        {
            List<string> emailList;
            List<string> invalidList = new List<string> { };
            string[] attachments = { };
            if (rbListType.Items.FindByValue("CSV").Selected)
            {
                emailList = tbEmailTo.Text.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                int count = 0;
                int total = emailList.Count;
                RadRadialGauge1.Scale.Max = total;
                RadRadialGauge1.Scale.Min = 0;
                //for (int i = 0; i < emailList.Count; i++)
                //{
                //    if (emailList[i].Contains('<'))
                //    {
                //        if (!Mail.IsValidEmailAddress(emailList[i].ToString().Split('<')[1].Replace(">", ""), PortalId))
                //        {
                //            emailList.RemoveAt(i);
                //            invalidList.Add(emailList[i].ToString());
                //        }
                //    }
                //    else
                //    {
                //        if (!Mail.IsValidEmailAddress(emailList[i].ToString(), PortalId))
                //        {
                //            emailList.RemoveAt(i);
                //            invalidList.Add(emailList[i].ToString());
                //        }
                //    }
                //}
                Parallel.ForEach(emailList, email =>
                {
                    Mail.SendMail(tbEmailFrom.Text, email.ToString(), tbEmailCC.Text, tbEmailBC.Text, tbReplyTo.Text, DotNetNuke.Services.Mail.MailPriority.Normal,
                        tbSubject.Text, MailFormat.Html, System.Text.Encoding.UTF8, tbEmailBody.Text, attachments, smtpServer, smtpAuthentication, smtpUsername, smtpPassword, false);
                    Interlocked.Increment(ref count);
                });
                RadRadialGauge1.Pointer.Value = count;
            }
            if (rbListType.Items.FindByValue("Excel").Selected)
            {
                emailList = tbEmailTo.Text.Split(new string[] { "'n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                int count = 0;
                int total = emailList.Count;
                RadRadialGauge1.Scale.Max = total;
                RadRadialGauge1.Scale.Min = 0;
                //for (int i = 0; i < emailList.Count; i++)
                //{
                //    if (emailList[i].Contains('<'))
                //    {
                //        if (!Mail.IsValidEmailAddress(emailList[i].ToString().Split('<')[1].Replace(">", ""), PortalId))
                //        {
                //            emailList.RemoveAt(i);
                //            invalidList.Add(emailList[i].ToString());
                //        }
                //    }
                //    else
                //    {
                //        if (!Mail.IsValidEmailAddress(emailList[i].ToString(), PortalId))
                //        {
                //            emailList.RemoveAt(i);
                //            invalidList.Add(emailList[i].ToString());
                //        }
                //    }
                //}

                Parallel.ForEach(emailList, email =>
                {
                    Mail.SendMail(tbEmailFrom.Text, email.ToString(), tbEmailCC.Text, tbEmailBC.Text, tbReplyTo.Text, DotNetNuke.Services.Mail.MailPriority.Normal,
                       tbSubject.Text, MailFormat.Html, System.Text.Encoding.UTF8, tbEmailBody.Text, attachments, smtpServer, smtpAuthentication, smtpUsername, smtpPassword, false);
                    Interlocked.Increment(ref count);
                });
                RadRadialGauge1.Pointer.Value = count;
            }
            throw new SmtpException();
        }
    }
    catch (SmtpException smtp)
    {
        RadRadialGauge1.Pointer.Value = RadRadialGauge1.Pointer.Value - 1;
        var email = smtp.
        Exceptions.LogException(smtp);
    }
    catch (Exception ex)
    {
        Exceptions.LogException(ex);
    }
}

找出哪个Parallel ForEach线程抛出了异常

您可以按照Yuval Itzchakov的建议将其封装在try catch中。然后在最后errorList将填充所有无法发送的电子邮件地址。

var errorList = new ConcurrentBag<string>();
Parallel.ForEach(emailList, email =>
{
     try
     {
         Mail.SendMail(tbEmailFrom.Text, email.ToString(), tbEmailCC.Text, tbEmailBC.Text, tbReplyTo.Text, DotNetNuke.Services.Mail.MailPriority.Normal,
         tbSubject.Text, MailFormat.Html, System.Text.Encoding.UTF8, tbEmailBody.Text, attachments, smtpServer, smtpAuthentication, smtpUsername, smtpPassword, false);
         Interlocked.Increment(ref count);  
     }
     catch(SmtpException stmp)
     {
         Exceptions.LogException(smtp);
         errorList.Add(email);
     }   
});
RadRadialGauge1.Pointer.Value = count;

您可以配置Visual Studio调试器(我假设您正在使用)在抛出异常时立即中断。

打开Debug菜单,单击Exceptions项。使用Find按钮,搜索SmtpException,并单击"Thrown"列中的复选框。当您再次执行程序时,它将显示它发生的确切时刻,以及调用堆栈、局部变量等。