0

I have an issue with this simple async code. The execution goes througth the TestAsync action and goes througth the delay method but when the delay method returns nothing else happens. it seems like blocked for some reason.

public async Task<ActionResult> TestAsync()
{
     try
     {
         var res = await doLongOperation();
         return RedirectToAction("Index");
     }
     catch (Exception e) { }
 }

 private Task<bool> doLongOperation()
 {
     var test = new Task<bool>(() => { /*do the long operation..*/ return true; });
     return test;
 }

Any suggestion?

2 Answers 2

1

new Task<bool>(() => true) returns a task that is not started yet. This is an old syntax that requires you to invoke the Start method on the task. If you don't invoke the Start method, the task will never complete, and thus asynchronous execution will never continue (await will asynchronously wait forever).

If you simply want to delay execution, then simply use Task.Delay like this:

private async Task<bool> delay()
{
    await Task.Delay(1000);

    return true;
}

In general, it is always recommended to create tasks that are already started upon creation.

If you want to create a task that runs on a thread-pool thread, then use the Task.Run method.

Sign up to request clarification or add additional context in comments.

15 Comments

I don't want to delay, it should be a DoSomething task. So, how can I start the task on creation.?
I would discourage us from leading someone on to think that using Task.Run() in an asp.net application would be a viable solution, as that's really just killing the scalability of their application since it needlessly takes up another thread pool thread on something that is actually a synchronous action. @DevT, I encourage you to just spend time studying the async/await pattern so you can get a strong basis on what Async is/isn't.
@DevT, what would the DoSomething task do and why would you want to make it asynchronous? does it involve any IO?
@DevT in the exact scenario you mentioned you would await the async method call to execute that query on the database. Many database API's now have an Async version of their synchronous call to the database that you can await directly. This would be the perfect use of async/await. Your request thread would be free while the database server is doing work executing your query, and then resume when it finished.
@DevT You could release the current thread, but basically your only other option is to then take up another thread from the thread pool on your web server, essentially taking up more resources to do something you only have synchronous actions to use. Say you wrap up your sync database call in Task.Run(), your current request thread would get freed, but then another one would be taken from your webserver to execute the task created by the .Run() call.
|
0

The method posted in your question doesn't work properly because the task is not started, there's nothing finishing so your await is waiting indefinitely. If you want to use Async it's best to use async all the way down, especially in an ASP.NET context. If you are not doing anything that is truly asynchronous, then in a web development context, just allow it to be synchronous.

I honestly believe the most good that will be done here is for you to spend time studying async/await, an excellent resource is found here: Stephen Cleary Blog on Async

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.