Reputation: 1187
In Asp.Net MVC project I have an Asynchronus controller, wand I also have an action of type Task to implement Asynchronus functionality.
Well I have the following code,
Action startMethod = delegate() { CreateUserFunctionality(user.UserID); };
return Task.Factory.StartNew(startMethod).ContinueWith(
t =>
{
return (ActionResult)RedirectToAction("Profile", "UserProfile");
});
And I also has, CreateUserFunctionality as
public void CreateUserFunctionality(int UsrId)
{
if (UsrId !=0)
{
_userService.NewFunctionality(UsrId);
}
}
I expect that the function will start the job, CreateUserFunctionality
and returns the view for me which is written in ContinueWith
function. However in my case that continuewith part always waits for the CreateUserFunctionality
to complete. My aim was to return the view immediatly and not wait for CreateUserFunctionality
to finish, as It should continue in background. Please let me find out what I am missing here.
Upvotes: 0
Views: 2723
Reputation: 101681
Ofcourse it will wait because you are using ContinueWith
method.If you don't want to wait just start your task, and leave it.Return your ActionResult immediately:
var task = Task.Factory.StartNew(startMethod);
return RedirectToAction("Profile", "UserProfile");
From documentation:
Task.ContinueWith Method : Creates a continuation that executes asynchronously when the target Task completes.
So it is working as expected.
Upvotes: 1