I have a tendency to forget that people don't necessarily know everything (in counselling we called it "contextual framing" - basically aligning your mindset with that of the person you're counselling). With this in mind, here's a simple one which I forgot many prople don't know - how do you call a method asynchronously, e.g., how do you send an email whilst not blocking the thread of an ASP.NET page? Well, .NET makes this REALLY easy to do.

So, you probably know that sending email in ASP.NET is dead easy:

using System;

using System.Web.Mail;

namespace ExtraControls

{

            ///

            /// Summary description for MailSender.

            ///

            public class MailSender

            {

                        public void SendMail(string fromAddress,string toAddress, string subjectLine,string messageText)

                        {

                                    SmtpMail.Send(fromAddress,toAddress,subjectLine,messageText);

                        }

            }

}

Right, so this sends to localhost, simple email, how do you make this Asynchronous?

using System;

using System.Web.Mail;

namespace ExtraControls

{

            ///

            /// Summary description for MailSender.

            ///

            public class MailSender

            {

                        public void Main(string fromAddress,string toAddress, string subjectLine,string messageText)

                        {

                                    SendMailDelegate del = new SendMailDelegate(SendMail);

                        AsyncCallback callback = new AsyncCallback(EndSendMail);

                                    IAsyncResult ar = del.BeginInvoke(fromAddress,toAddress,subjectLine,messageText,callback,null);

                        }

                        public void EndSendMail(IAsyncResult ar)

                        {

                                    SendMailDelegate del = (SendMailDelegate)ar.AsyncState;

                                    try

                                    {

                                                del.EndInvoke(ar);

                                    }

                                    catch

                                    {

                                                //Or handle the exception in here...

                                                throw;

                                    }

                        }

                        public delegate void SendMailDelegate(string fromAddress,string toAddress, string subjectLine,string messageText);

                        public void SendMail(string fromAddress,string toAddress, string subjectLine,string messageText)

                        {

                                    SmtpMail.Send(fromAddress,toAddress,subjectLine,messageText);

                        }

            }

}

Right, so quite a few things added here - you'll see that there's a delegate called SendMailDelegate - which essentially acts as a method pointer letting you fire the SendMail method on a new thread. The actual 'firing' takes place using the del.BeginInvoke(...) line. The AsyncCallback specified before that line gives us a hook into a method which fires after the SendMail method completes - this is where you can handle errors as I've indicated, it's also where you can call the EndInvoke for the delegate.

Now it strictly isn't necessary to use the EndSendMail method to call the 'EndInvoke' - but it is best practice to avoif potential memory leaks. So that's it, whenever you want to do something asynchronously that's the essential pattern you'll want to use (there are others such as starting new threads etc...but this is the most common).