I have a method that handles sending of emails. I wrote it so that it would run asynchronously, so that it won’t slow down the web app. It looks like this:
protected static void SendMail(string to, string subject, string body)
{
try
{
using (var bgw = new BackgroundWorker())
{
bgw.DoWork += new DoWorkEventHandler(delegate
{
try
{
Thread.Sleep(15000);
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress(AdminEmail,
AdminName);
message.To.Add(new MailAddress(to));
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = false;
SmtpClient mailClient = new SmtpClient();
mailClient.Send(message);
}
}
catch (Exception ex)
{
Utils.Log(ex);
}
});
bgw.RunWorkerAsync();
}
}
catch (Exception ex)
{
Utils.Log(ex);
}
}
I added the Thread.Sleep(15000) to see if it works. To my surprise, it didn’t. For some reason, the web request doesn’t return until the email thread is done executing. When I step through the code, it runs through the code right away without waiting and calls the return method on the controller as expected. But the response is actually never sent to the server until the thread completes - which defies the whole point of it being asynchronous.
What am I doing wrong? Is this a bug in the MVC framework? Or did I just overlook something?
[UPDATE]
I am not sure what’s wrong with the code above but I re-wrote it as show below and it works very well.
protected static void SendMail(string to, string subject, string body)
{
try
{
var t1 = new Thread(SendMailAsync);
t1.Start(new string[] {to, subject, body});
}
catch (Exception ex)
{
Utils.Log(ex);
}
}
private static void SendMailAsync(object emailInfo)
{
try
{
var paramArray = emailInfo as string[];
if (paramArray != null)
{
var to = paramArray[0];
var subject = paramArray[1];
var body = paramArray[2];
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress(AdminEmail,
AdminName);
message.To.Add(new MailAddress(to));
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = false;
var mailClient = new SmtpClient();
mailClient.Send(message);
}
}
}
catch (Exception ex)
{
Utils.Log(ex);
}
}
source: emadibrahim
Related Stuff
-
MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.
-
Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone
-
TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support
-
ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.
-
Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.
-
Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software
- PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.
Be the first ... |Add your comment.
Your Comment ...
Name (required)
Email (required, hidden)
Website
