Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

Async task in Asp.net MVC Example

What is Asynchronous programming?
Asynchronous programming is writing code in a way that will allow multiple things to occur in application at the same time without blocking the GUI, or waiting for other task to complete.
When do we need Asynchronous calls?
Asynchronous calls are required for any long running task, think of a task that will Fetch 10000 records from database and send customized email to each user

Required namespace
using System.Threading;
using System.Threading.Tasks;

Async task implementation in Asp.net MVC Example


Notice some keywords like async, await , task

First we write few time taking tasks, here just to create some time gap we have added await Task.Delay(1000), but in real life you need to write actual code to fetch data from database, sending emails etc.
public async Task<int> LongTask1()
{
await Task.Delay(1000);
return 50;
}
public async Task<string> LongTask2()
{
await Task.Delay(1000);
return "WTR Test";
}
public async Task<int> LongTask3()
{
await Task.Delay(1000);
return 50;
     }

now call all above task from an ActionResult
public async Task<ActionResult> Index() 
{
Task[] taskArray = new Task[3]; 

taskArray[0] = LongTask1();
taskArray[1] = LongTask2();
taskArray[2] = LongTask3();

// Now, we await them all.
await Task.WhenAll(taskArray);

return View(); 
}
Even though all task will take some time to complete, but our GUI will remain free to do additional task

We have multiple waiting options with task
await task Wait/await for a task to complete
Get the result of a completed task
await Task.WhenAny Wait/await for one of a collection of tasks to complete
await Task.WhenAll Wait/await for every one of a collection of tasks to complete
await Task.Delay Wait/await for a period of time
Task.Run Set the Task on queue to run on threadPool

Async ActionResult in Asp.net MVC

Here we define an Async ActionResult in Asp.net MVC and learn how to work with async,await, Delay.
Following ActionResult are written in public class aspnetmvcController : Controller
// this is normal ActionResult
public ActionResult asynctask()
{
    return View();
}
// Here is how we make that async ActionResult
public async Task<ctionResult> asynctask()
{
    await Task.Delay(10);

    return View();
}

Now let's try to fetch some data asynchronously from database of any other data source to display on our page, so the goal is to keep the page free while data is loading, user should be able to click on other links freely.

public async Task<ActionResult> asynctask()
{
List<blogObj> blogList = await Task.Run(() => CFD.GetBlogList(Server.MapPath("~/configs/homepage.xml")));
         
ViewBag.BlogList = blogList;
            
await Task.Delay(10);
return View();
}

Notice await Task.Run(() => DataSourceMethodName())

You should read another Async Await Task Example in Console Application


Comment
Name Email Website
Subscribe
 
Async task in Asp.net MVC

Group Training