Tuesday, July 2, 2013

How effectively maintain long-running operations in MVC

While some processes running, if a request makes a network call that requires some processing time to complete, the request takes some processing time whether it is performed synchronously or asynchronously.

During an asynchronous call:

Server is not blocked from responding to other requests while it waits for the first request to complete.
Therefore, asynchronous requests prevent request queuing when there are many requests that invoke long-running operations.

Asynchronous action occurs :

Async pipeline

1) The Web server gets a thread from the thread pool
2) The worker thread is returned to the thread pool to service another Web request.
3) When the asynchronous operation is complete, it notifies ASP.NET & repeat 1st step again.

Synchronous applicable for Simplicity is more important than efficiency as well as simple or short-running process.

Asynchronous applicable for Parallelism is more important than simplicity of code as well as a long-running requested process.


Deriving the controller from AsyncController

async method provides a convenient way to do potentially long-running work without blocking the caller's thread. The caller of an async method can resume its work without waiting for the async method to finish.

await keyword does not block the thread until the task is complete. It signs up the rest of the method as a callback on the task, and immediately returns. When the awaited task eventually completes, it will invoke that callback and thus resume the execution of the method right where it left off.

Task<ActionResult> will not be scheduled for execution until the current task has completed, whether it completes due to running to completion successfully, faulting due to an unhandled exception, or exiting out early due to being canceled.

ContinueWith() can happen in any order. All you can be sure about is that they both happen only after the Task completes. If you want to wait until the continuation completes, you can do that by waiting on the Task returned from ContinueWith().

Thread.Sleep method blocks the current thread for the specified number of milliseconds.

All the above its better to have an idea f when to use Asynchronous call.


No comments:

Post a Comment