• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

ASP.NET MVC & Threads ...

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:

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

 View Full Story.
Posted at 09:51:45 am | Permalink | Posted in .Net  

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


Top Stuff

e-messenger

MessengerFX

eBuddy

ILoveIM

AIM Express

Top 20 Ruby CMS


Our Partners

Facebook Applications

Ajax Projects

Web 2.0 Sites

Webloglines

Human Development Handbook

Software Development Company

Ajaxlines

Stock Exchange Chat


About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (111)
  • Articles (85)
  • Bookmarking (35)
  • Calendar (19)
  • Chat (39)
  • ColdFusion (3)
  • CSS (41)
  • Email (23)
  • Facebook (23)
  • Flash (15)
  • Games (6)
  • Google (28)
  • Html (14)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (36)
  • Javascript (171)
  • JSON (21)
  • Perl (2)
  • PHP (88)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (1)
  • Ruby (10)
  • Storage (4)
  • Toolkits (90)
  • Tutorials (199)
  • UI (12)
  • Utilities (167)
  • Web2.0 (13)
  • XmlHttpRequest (20)
  • YUI (4)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange