2

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 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Drew, especially on the warning about exception handling. A few requests for clarification. 1)Are there any differences in behavior in wrapping the call to SendMail in a try/catch block vs wrapping the body of SendMail in a try/catch block vs using a continuation with the OnlyOnFaulted option? I did a little bit of reading and found that when catching outside of the method you should look for an AggregateException [i think]
@jm2 Yes, that's true. With the try/catch directly in the SendMail task itself it's just straight up synchronous code so the exceptions will propagate like vanilla .NET code. If you use the ContinuationApproach, any exception that occurs under the execution of the SendMail Task will be wrapped by AggregateException. If you know your SendMail is completely synchronous underneath, then you can easily/safely just pull the original exception from the AggregateException::InnerException property if you need to inspect it and react to different types.
Terse part since comments are broken 2)Are you saying that accessing the values directly is unsafe because they are subject to change? The controls and their values are available I'm assuming due to SynchronizationContext maintaining/restoring page state.
Accessing anything related to ASP.NET off the ASP.NET synchronization context could result in an error because the underlying HttpContext instance may not be there. If you use explicit TaskScheduler.FromCurrentSyncrhonizationContext() the thread will be invoked on a thread configured by the AspNetSynchronizationContext class that can safely access that stuff, but keep in mind you are potentially keeping an awful lot of baggage around at that point. That's why I just recommend hoisting the values you need for SendMail out of the current request at the point where you schedule that work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.