I have a controller action method that returns JSON result. In this controller action, i want to do asyc and await for a long running operation without waiting for the JSON result to return to the browser.
I have below sample code -
`public JsonResult GetAjaxResultContent(string id)
{
List<TreeViewItemModel> items = Test();
//use the below long running method to do async and await operation.
CallLongRunningMethod();
//i want this to be returned below and not wait for long running operation to complete
return Json(items, JsonRequestBehavior.AllowGet);
}
private static async void CallLongRunningMethod()
{
string result = await LongRunningMethodAsync("World");
}
private static Task<string> LongRunningMethodAsync(string message)
{
return Task.Run<string>(() => LongRunningMethod(message));
}
private static string LongRunningMethod(string message)
{
for (long i = 1; i < 10000000000; i++)
{
}
return "Hello " + message;
}
`
However, the controller action waits untill it finishes the long running method and then returns the json result.