3

Is there a way to make a controller return something to the user first then execute another method?

I have a process that is very time consuming. I'd like to send back a quick JSON response to the user then process.

2 Answers 2

5

In that scenario, you might want to consider doing the ongoing task on a worker thread. The MVC loop must complete in order to return the response to the caller. Note that background threads will not have access to the request context, so you must capture anything you need. Note also that you cannot return anything to the caller once the http context has exited - you will need the caller to call back to get any extra data.

Sign up to request clarification or add additional context in comments.

4 Comments

Does anyone know where i can find an example of this? thanks. im not sure how to start a worker thread.
Check out C# 4.0 Task class: msdn.microsoft.com/en-us/library/…
@Marc Is it safe to use in a web app?
@Jason - sure! just don't saturate it. If your tasks are going to take excessively long, a custom thread with queue would be preferable
2

This may serve as a good example: http://tpeczek.com/2010/07/reporting-server-side-operation.html

Comments

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.