Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7530475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:55:15+00:00 2026-05-30T04:55:15+00:00

I have a webform where I am using TPL to send email in the

  • 0

I have a webform where I am using TPL to send email in the background since our SMTP server is slow and many users end up hammering away on the submit button out of frustration. In the past I had used System.Threading and static methods to accomplish a task that was similar – in .NET3.5, my code looked like this:

Thread t = new Thread(new ParameterizedThreadStart(SendEmail));
t.Start(txtEmail.Text);

Where SendEmail’s signature was public static void AddEmailToMailingListInBackground(object EmailString) and as I remember the method had to be static, and I had to pass the value of the TextBox txtEmail to the asynchronous method or risk losing access to the control’s value as the page lifecycle continued independently.

Now when using System.Threading.Tasks my code looks like this:

Task.Factory.StartNew(() => SendEmail(), TaskCreationOptions.LongRunning);

and the signature of SendEmail is private void SendEmail() and I access the Text property of txtEmail directly within that method.

There are two major differences that I see here. First, my asynchronous method no longer has to be static. Second, I am able to access the control values of the Page within the method long after the Page’s lifecycle would have completed if I were using Threads. These two points lead me to believe that the Page is being kept alive until all Tasks have completed or the Page lifecycle has completed, whichever comes last. I’ve tested this by debugging and stepping through the asynchronous method – the response is sent to the browser yet I’m still able to step through and access controls and their values. This MSDN article helps a little but still doesn’t really solidify my understanding of what’s happening with TPL vs the pre-.NET4 way of executing asynchronous calls.

Can anyone tell me if my thinking is correct and this is dependable behavior when using TPL with ASP.NET?
Would anyone care to elaborate further?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:55:17+00:00Added an answer on May 30, 2026 at 4:55 am

    It is technically not safe to access ASP.NET objects off the ASP.NET thread. You would be much better off extracting the details that SendMail needs from the page/request and passing them through via the closure.

    Also, you need to make sure you “observe” any exceptions that could happen in SendMail or the TPL will raise an exception that will crash your entire web application. You can do this with a try/catch around the SendMail call itself or chain on another continuation with TaskContinuationOptions.OnlyOnFaulted option. In the continuation approach, you just need to access the Exception property of the antecedent, presumably to log it, in order for TPL to know you’ve “observed” the exception.

    So to put this all together it might look like this:

    string userEmail = userEmailTextBox.Text;
    // whatever else SendMail might need from the page/request
    
    Task.Factory.StartNew(() => SendMail(userEmail))
                .ContinueWith(sendEmailAntecedent =>
                              {
                                  Trace.TraceError(sendEmailAntecedent.Exception.ToString());
                              },
                              TaskContinuationOptions.OnlyOnFaulted|TaskContinuationOptions.ExecuteSynchronously);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET webform which I want to validate Client-Side and Server-Side, using
I have a webform that is a survey for users to fill using a
I have 2 dropdown lists on my webform and using jquery trying to filter/reset
I have implemented remember me option in my asp.net webform by using this, protected
I am using VS2005 C# ASP.NET. I have a webform which contains a list
I am absolute beginner to drupal. I have added a contact form (using Webform
I have an ASP.NET application that authenticates users using Ldap against active directory. This
I have a simple webform that will allow unauthenticated users to input their information,
I have an button in my Jquery mobile page, which is using asp.net webform.
I have the following on my interface / webform: <div id=mydiv class=forceHeight runat=server />

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.