1

I've been reading up on asynchronous function / web pages for ASP.NET 3.5 (we're not using v4 yet) but the ones I've seen all focus on performing a background task but then still coming back to finish up and send the response, after the task is complete.

What I want to do is simply kick off the background task (in this case a call to a web service) but then return a response to the browser immediately - ie. without waiting for the asynchronous task to complete. I don't even need to know if it succeeds or not.

What is the best way to do that? I can't seem to find examples of kicking off what you might call an "orphan" background task in ASP.NET.

I thought of doing it via a javascript Ajax call on the page I show to the user, but the information passed to the web service is sensitive, so that's out of the question. But that kind of illustrates what I want to do.

[ed] Another idea: Is there an event in the ASP.NET model I can use which occurs after the response has been sent to the browser and the connection closed? ie. So more processing can occur without the user waiting for it?

2 Answers 2

2

You can use threads...if you don't care about the response, just use:

try
{
Thread t = new Thread(new ThreadStart(yourMethodName));
t.Start();
}
catch{}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, didn't realise it was that easy. :) However, after investigating the options, I'm unclear on the difference between a) Dim th As New Thread(New ThreadStart(Function() DoWork(param))), b) Dim th As New Thread(DirectCast(Function() DoWork(param), ThreadStart)), and c) ThreadPool.QueueUserWorkItem(Function() DoWork(param)). They all seem to function the same.
If i understand the things right, using ThreadPool.QueueUserWorkItem is bad for ASP.NET since it will use a threads dedicated for processing requests and loading them too much will cause problems will your site. When you using threads, it not using ASP.NET thread pool so you can steel process the same amount of request. But as @ss4526 said, the best thing it is to separate the things, but it's totally another story :) Just instead of MSMQ, use WCF :)
1

I have explored this topic quite a bit and have not found a great answer or technique within ASP.Net that is truly asynchronous. The problem seems to be that even if you kick off an Async task using any of the popular techniques listed below:

  • QueueUserWorkItem method
  • Async helper methods that start with Begin/End
  • System.Thread
  • There are probably more that I am not aware of...

You are still creating a situation where the limited threads available to your web app are being blocked. The UI may not appear blocked but another thread in the "ThreadPool" definitely is. This can cause your application to choke up during periods of high traffic when every thread needs to be serving up request quickly.

Although it is extra work and will have its own set of maintenance issues. I would recommend offloading any long running tasks to an MSMQ queue and then utilize a Windows Service to process tasks on the queue. This will allow you web app to utilize all available threads for serving up requests and the tasks you offloaded will keep getting processed in the background. Another benefit of using MSMQ is that you can actually send the tasks to a completely different server. This has 2 benefits:

  • You are not using up resources on the Web Server that your app runs on.
  • Potential issues with MSMQ and the Service will not necessarily impact the web app and can be debugged and handled without toucing the production server that your application is running on.

As I said, I know this is a bunch of extra work but if you have a high performing site then this would be a safer bet. The goal is to keep the web server running as smoothly as possible without bogging it down with long running tasks.

1 Comment

Thanks for that, however being on a shared server I'm limited in what can be achieved in an ideal situation. I've settled on kicking off a new thread. As the main thread (which serves the page) exits immediately upon starting the new thread, I assume there won't be a "loss" of a thread to the pool for IIS overall.

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.