• About Morris Development
  • A Focus for Cloud Efficiency
    • Microsoft Azure
    • Amazon Web Services
  • What our Clients Say
  • Our Products
    • PreschoolDB
    • WebinarDB
  • Contact Us

Morris Development

Custom System Development and Integration

October 9, 2011

Executing Asynchronous Functions from ASP.NET

Don’t make your users wait unnecessarily. For example, a page where you generate a PDF and send it to someone else. Hand the job off to an asynchronous process and move on.

There are a lot of ways this can be done, but the easiest seems to be using a Thread. However, you run into a problem with having parameters. A parameterizedThread can actually only have ONE parameter…. but it can be an object, so that means you can jam all sorts of cool things in there.

Here is a example of a parameterized thread

First, you make a nice class to hold your parameters:


    Class mailParams
        Public mTo As String
        Public mFrom As String
        Public subject As String
        Public body As String      
        Public attachmentMemoryStream As MemoryStream
        Public attachmentFileName As String
        Public archiveFile As String
    End Class

Then we have a subroutine attached to a button that does a fire-and-forget email. Basically, it just says, “send that..” and continues to process the page. You get refreshed practically instantly.

  
protected sub btnSendMail_OnClick(sender as object, e as eventargs) handles btnSendMail.click
    
    Dim objMailParams As New mailParams
    objMailParams.mTo = strTo
    objMailParams.mFrom = strFrom
    objMailParams.subject = strSubject
    objMailParams.body = strMessage
    

     Dim asyncMailObj As New System.Threading.ParameterizedThreadStart(AddressOf sendAnEmail)
     Dim t As New System.Threading.Thread(asyncMailObj)
     t.Start(objMailParams)

end sub

That ParameterizedThreadStart simply assigns a CPU thread to kick off the subroutine “sendAnEmail”. I preloaded the class of parameters and used the entire class as a single object parameter.

sub sendAnEmail(obj as object)
       Dim mp As mailParams = obj


        '===  I keep all my settings for mail and connection strings in a class called DataLayer.  
        Dim myServer As New DataLayer.mailSettings



        Dim objMM As New MailMessage
        Dim objSMTP As New SmtpClient
        
        Dim m As New MailMessage()
        Dim mTo As New System.Net.Mail.MailAddress(mp.mTo)
        Dim mFrom As New System.Net.Mail.MailAddress(mp.mFrom)
        

        objMM.To.Add(mTo)
        objMM.From = mFrom
        objMM.IsBodyHtml = False
        objMM.Priority = MailPriority.Normal
        objMM.Subject = mp.subject
        objMM.Body = mp.body
        objSMTP.Host = myServer.smtpServer
        objSMTP.DeliveryMethod = SmtpDeliveryMethod.Network
        objSMTP.Credentials = New System.Net.NetworkCredential(myServer.smtpUser, myServer.smtpPassword)
        objSMTP.EnableSsl = False
        objSMTP.Send(objMM)

end sub

Now, this is a very simple example. The reason I started doing this is because I was generating PDF invoices by screen captures, then sending an archive of the PDF up to Amazon’s S3 storage, then emailing the pdf to the client. Some of the invoices were 20 pages long. The server can handle the workload, but people couldn’t handle the wait. So, fire-and-forget.

Later on, if it becomes a problem, I’ll see about putting in some kind of confirmation emails that say whether or not the invoice actually got sent.

If it becomes a server memory issue, I’ll update this post with whatever solution I came up with next!

Article by MacGyver / ASP.net, Training / Asynchronous, email

About MacGyver

I've worked with database systems for over 20 years, and started my own company in 2000. Almost all my business consists of internal database systems, either ERP or CRM. My programming is primarily in Angular / Microsoft C# and MS SQL.

About This Site

Morris Development has been specializing in internal database system design and integration since 1999. We provide long-term management and support of secure data systems for many businesses as well as developing the more complex code structures for ERP systems like Intellievent, Apidas, and AVMS.

This site is primarily for our developers to keep track up various technologies and updates that are used by Morris Development.

Training

Integrating Angular Microsite with .Net

Private Data Caching with Google Storage

Continuous Deployment for Production Releases?

Azure Websites – the perfect Angular host

Angular 2

  • Angular 2 Authentication
  • Angular Command Line Interface
  • Material Design for Angular
  • Using Observables in Angular 2

Mentors

  • Ben Nadel
  • Dan Wahlin
  • Deborah Kurata
  • John Papa

Staff

  • Dan Morris

Training

  • Google Development Courses
  • Microsoft Virtual Academy
  • PluralSight
  • Test Deep Links

© 2025 · Morris Development