确保添加到Listview发生在每次方法执行之后
本文关键字:方法 执行 之后 添加 Listview 确保 | 更新日期: 2023-09-27 18:13:34
我创建了一个方法,将数据从数据库拉到datagridview,并从该datagridview,发送电子邮件的方法。我这样编码:
// TODO: Prepare the email addresses
private void SendEmail()
{
// Get the email addresses according to bio_id
var rdr = new EmployeeDataAccess().GetBioIdEmail();
while (rdr.HasRows && rdr.Read())
{
dgvBioIdList.Rows.Add(rdr["bio_id"].ToString(), rdr["email_add"].ToString());
}
// Get the time logs for each of the bio_id in the datagridview
Cursor.Current = Cursors.WaitCursor;
for (var i = 0; i < dgvBioIdList.Rows.Count; i++)
{
using (var cn = new DatabaseConnection().ConnectToMySql())
{
const string query = @"SELECT MIN(scan_time) as 'TimeIn', MAX(scan_time) as 'TimeOut',
TIMEDIFF(MAX(scan_time),MIN(scan_time)) as difference
FROM `filtered_dates`
WHERE date BETWEEN @fromDate AND @toDate AND bio_id = @bioId
GROUP BY date
ORDER BY date asc";
var cmd = new MySqlCommand(query, cn);
cn.Open();
cmd.Parameters.AddWithValue("@fromDate", dtpStart.Text);
cmd.Parameters.AddWithValue("@toDate", dtpStop.Text);
cmd.Parameters.AddWithValue("@bioId", dgvBioIdList.Rows[i].Cells[0].Value);
rdr = cmd.ExecuteReader();
// Create the message to send to the email
if (!rdr.HasRows) continue;
var message = "Time Logs for Bio ID: " + dgvBioIdList.Rows[i].Cells[0].Value;
message += Environment.NewLine;
message += @"Included dates: " + dtpStart.Text + @" to " + dtpStop.Text;
message += Environment.NewLine;
message += @"Email Address: " + dgvBioIdList.Rows[i].Cells[1].Value;
message += Environment.NewLine;
while (rdr.Read())
{
message += rdr["TimeIn"] + @" - " + rdr["TimeOut"] +@" = " +rdr["Difference"];
message += Environment.NewLine;
}
// Actual sending of the email
SendingEmail(dgvBioIdList.Rows[i].Cells[0].Value.ToString(),dgvBioIdList.Rows[i].Cells[1].Value.ToString(), message, txtUserName.Text, txtPassword.Text);
}
}
Cursor.Current = Cursors.Default;
}
// Sending of the E-mail and to list the status of the sending in a listview
private void SendingEmail(string bioId, string recipient, string body, string userName, string password)
{
var mail = new MailMessage();
var smtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(userName);
mail.To.Add(recipient);
mail.Subject = "Time Logs";
mail.Body = body;
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential(userName, password);
smtpServer.EnableSsl = true;
try
{
smtpServer.Send(mail);
string[] row = {bioId, recipient, "PASSED"};
var listViewItem = new ListViewItem(row);
lvEmailStatus.Items.Add(listViewItem);
//Console.WriteLine(@"Successfully sent mail to " + recipient);
}
catch (Exception ex)
{
string[] row = { bioId, recipient, "FAILED" };
var listViewItem = new ListViewItem(row);
lvEmailStatus.Items.Add(listViewItem);
Console.WriteLine(@"Failed sending mail " + recipient);
return;
}
}
这段代码工作得很好,除了listview应该通过datagridview中的每次行迭代填充。发生的是;它首先向datagridview中的每个人发送电子邮件,当它这样做时,程序会有些冻结,但你仍然可以最小化并关闭窗口。一旦发送完成,listview将在这段时间内填写数据以及发送的状态(如果发送成功或失败)。但如果我在控制台中这样做。Writeline,我可以在去datagridview的下一行之前看到发送的状态!它应该是这样的-(选择行)-(发送电子邮件)-(查看填充的listview) -(转到下一行)-(发送电子邮件)-(查看填充的listview) ....循环直到datagridview的最后一行。
非常感谢您的帮助。
这是因为你的方法是从UI线程调用的,但你在进程中不释放UI线程。我更喜欢使用async
await
来保持UI线程在繁忙的进程中可用。