Reputation: 21368
I make an ajax request to invoke a controller action like so:
Controller:
[AsyncTimeout(8000, Order = 3)]
[HandleError(ExceptionType = typeof(TimeoutException), Order = 4, View = "TimeoutError")]
public async Task<JsonResult> RemovePackageAsync(CancellationToken cancellationToken, string packageId, ICollection<int> ids)
{
PackageRemoveItem removeItem = new PackageRemoveItem()
.............blah blah blah
await this.deviceRepository.RemovePackageAsync(removeItem);
return this.Json(JsonConvert.SerializeObject(removeItem.Machines));
}
Method:
public async Task RemovePackageAsync(PackageRemoveItem removeItem, CancellationToken cancelToken = default(CancellationToken))
{
await Task.Run(() =>
{
int[] machines = removeItem.Machines.Keys.ToArray();
.......
});
}
However, when I run this in debug my ajax response waits until my "RemovePackageAsync" finishes. What I'm trying to achieve here is to have this method be called on a different thread, let it execute and NOT wait for it to finish but to get "true" response right away from the controller.
ajax call => controller => call method on new thread, do NOT wait for it to finish BUT return true => let method run as long as it needs. and I get to do other stuff
Upvotes: 1
Views: 3041
Reputation: 16038
Asynchronous controllers work asynchronous just inside the webserver to free threads from the threadpool to handle concurrent requests. The browser still waits synchronous for the result to be returned by your action.
What you need is a standard controller action which just spawns a thread or a task and returns immediately.
But that way you can't return any resulting data.
Upvotes: 3