1

So here is my issue. I have my email application sending out email, at the end of the send i have it writing the status of the message to the rich text box. here is the probelm. Say I am sending out 10 emails, the richtext box doesn't update on every iteration. When all 10 emails are sent, then the box is updated with the sent messages. It seems that the appliation is frozen while going though the four loop.

for (int count = 0; count < numEmailToSend; count++)
            {
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                if (typeSelectedItem == "text/html") { message.IsBodyHtml = true; }
                if (typeSelectedItem == "text/plain") { message.IsBodyHtml = false; }

                message.To.Add(emailAddress);

                if (txtCC.Text != "")
                {message.CC.Add(txtCC.Text);}
                if (txtBCC.Text != "")
                {message.Bcc.Add(txtBCC.Text);}

                message.Subject = subject;
                message.From = new System.Net.Mail.MailAddress(emailSender);
                message.Body = body;
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(server);
                rtbStatus.Text = rtbStatus.Text + "Starting to Send Message\r\n";
                Thread.Sleep(avgDelay * 1000);
                //smtp.Send(message);
                smtp.SendAsync(message,1);
                rtbStatus.Text = rtbStatus.Text + "Message Sent\r\n";
            }

Any ideas?

1 Answer 1

3

You are correct.
While your code is running in the UI thread, the UI cannot update.

You should run your code on a background thread instead.
You can use the BackgroundWorker component to make this simpler.

Note that you cannot interact with the UI from a background thread; to update the textbox, you should either call BeginInvoke with an anonymous method that updates the text, or use BackgroundWorker's built-in progress reporting functionality.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.