Como dijo uno babuino camino a seguir es el enfoque Trabajador de otra manera si está utilizando .Net 4 o superior podría ser el uso de Task clase
clase de tareas simplifica la ejecución de código en el fondo y la interfaz de usuario de hilo según sea necesario . El uso de la clase de tareas se puede evitar escribir código adicional de la ambientación de eventos y devoluciones de llamada mediante el uso de tareas Continuation
Reed Copsey, Jr. tiene una muy buena series el paralelismo en .Net también echar un vistazo a lo
por ejemplo, una La forma sincrónica de hacer las cosas puede ser
//bad way to send emails to all people in list, that will freeze your UI
foreach (String to in toList)
{
bool hasSent = SendMail(from, "password", to, SubjectTextBox.Text, BodyTextBox.Text);
if (hasSent)
{
OutPutTextBox.appendText("Sent to: " + to);
}
else
{
OutPutTextBox.appendText("Failed to: " + to);
}
}
//good way using Task class which won't freeze your UI
string subject = SubjectTextBox.Text;
string body = BodyTextBox.Text;
var ui = TaskScheduler.FromCurrentSynchronizationContext();
List<Task> mails = new List<Task>();
foreach (string to in toList)
{
string target = to;
var t = Task.Factory.StartNew(() => SendMail(from, "password", target, subject, body))
.ContinueWith(task =>
{
if (task.Result)
{
OutPutTextBox.appendText("Sent to: " + to);
}
else
{
OutPutTextBox.appendText("Failed to: " + to);
}
}, ui);
}
Muchas gracias. La solución es muy útil – RSP