private static void SendEmailAsyncFinish(IAsyncResult vResult) //this function is called upon finish of the async operation. its only purpose is to close the generic pointer construction which is still using memory. { AsyncResult vResultTemp = vResult as AsyncResult; //cast vResult to AsyncResult delegateSendEmailAsync vDelegate = vResultTemp.AsyncDelegate as delegateSendEmailAsync; //receive the generic pointer construction try { vDelegate.EndInvoke(vResult); //wait for the async operation to finish and close it } catch (Exception vError) { //log error } }
private delegate void delegateSendEmailAsync(string vTemplateName, Dictionary <RecipientType, string> vAddresses, string vSubject, Dictionary <String, Object> vValues); //this is a generic pointer construction, it can point to any function using the given signature. it can execute asynchronously, that's why I use it. public static void SendEmailAsync(string vTemplateName, Dictionary <RecipientType, string> vAddresses, string vSubject, Dictionary <String, Object> vValues) { delegateSendEmailAsync vCallSendEmail = new delegateSendEmailAsync(SendEmail); //this uses the generic pointer to point to the function SendEmail vCallSendEmail.BeginInvoke(vTemplateName, vAddresses, vSubject, vValues, new AsyncCallback(SendEmailAsyncFinish), null); //the execution of function SendEmail is started asynchronously, when it finishes it calls SendEmailAsyncFinish }