如何从表单前面的backgroundWorker获取流程的每个状态

本文关键字:状态 获取 backgroundWorker 表单 前面 | 更新日期: 2023-09-27 18:29:38

我有一个简单的基于Windows的应用程序。我用BackgroundWorker一封接一封地发送邮件。以下是DoWork事件处理程序代码

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    MailsSample Mails = e.Argument as MailsSample;
    string readings = "To : " + Mails.To_EmailsID + "- ";
    char[] delimiterChars = { ',' };
    string[] Emails = Mails.To_EmailsID.Split(delimiterChars);
    for (int i = 0; i < Emails.Length; i++)
    {
        try
        {
            SmtpClient smtp = new SmtpClient(Mails.SMTP, Mails.PORT);
            MailMessage msg = new MailMessage(Mails.From_Email, Emails[i], Mails.subject, Mails.body);
            string mto = null;
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = new NetworkCredential(Mails.From_Email, Mails.password);
            smtp.EnableSsl = true;
            msg.IsBodyHtml = true;
            msg.Subject = Mails.subject;
            //if (textBox1.Text != "")
            //{
            //    Attachment at = new Attachment(textBox1.Text);
            //    msg.Attachments.Add(at);
            //}

            msg.Body = Mails.body;
            mto = Emails[i];
            msg.IsBodyHtml = true;
            smtp.Send(msg);

            readings += "Sent Successfully.";
        }
        catch (Exception ex)
        {
            readings += "Error : " + ex.Message;
        }
        //Mails.t.AppendText ( Environment.NewLine +readings);
    }
    e.Result = readings;
}

这里是for循环,我想从存储在"reads"变量中的for循环中获取每个状态。我想从任何文本框中一个接一个地在前面显示单个状态(读数)。在BackgroundWorker中可能吗?

如何从表单前面的backgroundWorker获取流程的每个状态

我得到答案

(sender as BackgroundWorker).ReportProgress((int)(100.0 / totalSteps * i), null);
  void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value =(int) e.ProgressPercentage;
    }