I have the following action method with async
and await
keywords:
[HttpPost]
public async Task<ActionResult> Save(ContactFormViewModel contactFormVM)
{
if (domain.SaveContactForm(contactFormVM) > 0)// saves data in database
{
bool result = await SendMails(contactFormVM);//need to execute this method asynchronously but it executes synchronously
return Json("success");
}
return Json("failure");
}
public async Task<bool> SendMails(ContactFormViewModel contactFormVM)
{
await Task.Delay(0);//how to use await keyword in this function?
domain.SendContactFormUserMail(contactFormVM);
domain.SendContactFormAdminMail(contactFormVM);
return true;
}
In the above code, once the database operation is finished I want to immediately return Json()
result and then call the SendMails()
method which should execute in the background. What changes should I make to the above code?
In the File menu, click New Project. In the "New Project" dialog box, under Project types, expand Visual C#, and then click "Web". In the Name box, type "Async", then click on Ok. Now, in the dialog box, click on "MVC" under the ASP.NET 4.5.
Asynchronous actions are best when your method is I/O, network-bound, or long-running and parallelizable. Another benefit of an asynchronous action is that it can be more easily canceled by the user than a synchronous request.
When to use Asynchronous Controller. Asynchronous action methods are useful when an action must perform several independent long running operations. Suppose we have three operations which takes 500, 600 and 700 milliseconds. With the synchronous call, total response time would be slightly more than 1800 milliseconds.
As a result, actions that complete quickly usually run synchronously. For example, action=getstatus is a synchronous action. Actions that can take a significant time to complete are usually asynchronous.
The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work.
It sounds like you don't want to wait for the result of SendMails. Think of async and await as tools to consume asynchronous APIs. Specifically, it's really useful to be able to "await" for the result of an "async" task. However, if you don't care about the result of your "async" (e.g. SendMails) task, then you don't need to "await" for the result (i.e. boolean).
Instead, you can simply use Task.Run to invoke your asynchronous task.
[HttpPost]
public async Task<ActionResult> Save(ContactFormViewModel contactFormVM) {
if (domain.SaveContactForm(contactFormVM) > 0) {// saves data in database
Task.Run(() => SendMails(contactFormVM));
return Json("success");
}
return Json("failure");
}
public void SendMails(ContactFormViewModel contactFormVM) {
domain.SendContactFormUserMail(contactFormVM);
domain.SendContactFormAdminMail(contactFormVM);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With