如何在ASP.NET中将Parallel.ForEach与网格视图结合使用

如何在ASP.NET中将Parallel.ForEach与网格视图结合使用,asp.net,parallel-processing,Asp.net,Parallel Processing,我已经创建了一个应用程序来发送大量邮件,但是我的应用程序花费了太多的时间来发送所有的电子邮件。这是我的密码: foreach (GridViewRow grow in GridView1.Rows) { string Emails = grow.Cells[0].Text.Trim(); string file = Server.MapPath("~/Mail.html"); string mailbo

我已经创建了一个应用程序来发送大量邮件,但是我的应用程序花费了太多的时间来发送所有的电子邮件。这是我的密码:

  foreach (GridViewRow grow in GridView1.Rows)
        {
            string Emails = grow.Cells[0].Text.Trim();

            string file = Server.MapPath("~/Mail.html");
            string mailbody = System.IO.File.ReadAllText(file);
            string to = Emails;
            string from = "xyz@gmail.com";
            MailMessage msg = new MailMessage(from, to);
            msg.Subject = "Auto Response Email";
            msg.Body = mailbody;
            msg.BodyEncoding = Encoding.UTF8;
            msg.IsBodyHtml = true;
            SmtpClient client = new SmtpClient("smtp.live.com", 25);
            client.UseDefaultCredentials = false;   
            System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("xyz@gmail.com", "password");
            client.EnableSsl = true;
            client.UseDefaultCredentials = true;
            client.Credentials = basicCredential;
            try
            {
                client.Send(msg);
                cnfrm.Text = "Email Sended Successfully";
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        } 
因此,对于它的解决方案,我使用Parallel.ForEach,但我发现自己无法在GridView中使用它

  Parallel.ForEach(GridView1.Rows, GridViewRow =>
        {

            string Emails = grow.Cells[0].Text.Trim();

            string file = Server.MapPath("~/Mail.html");
            string mailbody = System.IO.File.ReadAllText(file);
            string to = Emails;
            string from = "xyz@gmail.com";
            MailMessage msg = new MailMessage(from, to);
            msg.Subject = "Auto Response Email";
            msg.Body = mailbody;
            msg.BodyEncoding = Encoding.UTF8;
            msg.IsBodyHtml = true;
            SmtpClient client = new SmtpClient("smtp.live.com", 25);
            client.UseDefaultCredentials = false;
            System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("xyz689@gmail.com", "password");
            client.EnableSsl = true;
            client.UseDefaultCredentials = true;
            client.Credentials = basicCredential;
            try
            {
                client.Send(msg);
                cnfrm.Text = "Email Sended Successfully";
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        });

谁能告诉我怎么用它

我不认为可以使用多个线程访问Gridview行,在主线程中提取文本,然后使用foreach。另外,请查看如何与
ForEach一起使用

下面是一个可能的外观示例:

var emailtext = new List<string>()
foreach (GridViewRow grow in GridView1.Rows)
{
        emailtext.Add(grow.Cells[0].Text.Trim());
}

//split and load balance the List
Partitioner<string> partitioner = Partitioner.Create(emailtext, true); 
Parallel.ForEach(partitioner, mailtext =>
{
    //your email sending code here
    //NB: 'mailtext' is your string to send
}
var emailtext=新列表()
foreach(GridViewRow在GridView1.Rows中增长)
{
emailtext.Add(grow.Cells[0].Text.Trim());
}
//对列表进行拆分和负载平衡
Partitioner Partitioner=Partitioner.Create(emailtext,true);
Parallel.ForEach(分区器,mailtext=>
{
//您的电子邮件发送代码在这里
//注意:“mailtext”是要发送的字符串
}

<强>注:大多数邮件提供者限制您每次发送或每小时发送的电子邮件数量,因此您可能需要在您的应用程序中考虑这一点。

我建议在非并行庄园中提取内容,然后使用并行版本发送邮件。不访问来自不同线程的特殊处理的UI元素!y Sprinter252,你能给我一个同样的例子吗!!试图直接从UI元素生成电子邮件不是一个好的选择。你应该使用与生成GridView相同的源见Chris L的答案