c# - Updating textbox asynchronously -


I am creating an email software that sends emails to some accounts. I want to send new emails or fail every time. But the text box shows me the report after sending all the emails, if the tilist is much bigger than 30+ emails, the app screen becomes white and after sending all the emails, the GUI comes back with the updated Output Textbox code inside the SendButton_Click method.

  foreach (string to toolist) {bool hasSent = SendMail (from "password", from, SubjectTextBox.Text, BodyTextBox.Text); If (hasent) {OutPutTextBox.appendText ("Sent:" + to); } And {OutPutTextBox.appendText ("Failed to:" + to); }}  

What do you really want to do SendMail There are many ways to do this asynchronously. In Net 4.0 I recommend starting a work object in my foreach loop, and suggests to continue one task for each one in the UI thread:

  string theme = topic textbox. Text; String body = bodytextbox. Text; Var ui = TaskScheduler.FromCurrentSynchronizationContext (); & Lt; Tasks & gt; Mail = new list & amp; Work; Tasks & gt; (); Forth (string to toolist) {string target = to; Var t = Task. Factor Start (with = ">"> Sendmail (from, "password", target, subject, body)) with contact (function => {(results.) {OutPutTextBox.appendText ("Sent copy:" + To);} and {OutPutTextBox.appendText ("Failed to:" +});}}, ui); Mails.Add (t); } Work. Structure when all (Mail Toure (), _ = & gt; {/ * something * /});  

(The syntax can be a bit far away; I did not compile it).


Comments