2

I have an async method and I want to get a message from the same method when it gets done.

Below is the code

 static async Task<string> pizza()
        {
            await Task.Delay(10);
            for (int i = 0; i < 100; i++)
            {
                //Console.WriteLine("Processing pizza...");
            }
            return "Pizza is ready";

        } 

And I want to get receive this message at calling point

....... code 
Task t=null;
            switch (option)
            {
                case 1:
                    {

                        await Task.Run(() =>
                        {

                         t=pizza();// asynchronous method  

                        });
                        Console.WriteLine(t.ToString());
                    }
                    break;
 .... other code
4
  • 1
    Just use await Commented May 23, 2017 at 6:48
  • any reason why you accepted a wrong answer, when a correct one is available from the beginning? Commented May 23, 2017 at 7:56
  • 1
    @Eqra: You may find my async intro helpful. Commented May 23, 2017 at 13:37
  • @StephenCleary, Yes sir thanks a lot, I have gone this. Commented May 24, 2017 at 3:54

4 Answers 4

8

Do you need the Task.Run part? You can await of you are inside an async method:

var result = await pizza();

Console.WriteLine(result);

You can also call result:

var result = pizza().Result;
Sign up to request clarification or add additional context in comments.

3 Comments

Would it run asynchronously? As I have run multiple articles but I am not getting when to use Task.run or when to use await task.
@Eqra there is no ambiguity. Task.Run runs something in the background. await only awaits for the result of an asynchronous operation. Your method already returns a Task
:( Alright. I try to understand, though I am trying this since days!
5

You can either

var pizza = await pizza();

Or

var pizza = pizza().GetAwaiter().GetResult();

3 Comments

I have done using something like this Console.WriteLine(await Task.Run(() => pizza()));
That looks complicated and isn't neccecary.
GetAwaiter is internal method and shouldn't be called
-1

You can use:

var returnValue = string.Empty;
    {
        returnValue = await Pizza();
    }
    Console.WriteLine(returnValue);

It will still behave asynchronously. When await statement is encountered, control will return to main thread and execution will not be paused. After a while when Pizza method has returned executing, state machine will automatically execute the returnValue output

You don't need to use Task.Run just for awaiting. You just need to await the async method. Then later on you can use returnValue wherever you want to.

8 Comments

No, that's what await is for. .Result will block until the task returns which is a bad idea unless you are absolutely certain that the task has already finished
But It won't remain asynchronous then. I want to do it Asynchronously.
@PanagiotisKanavos I didn't say that t.Result needs to be awaited. I meant that t.Result can be used after awaiting the method. In that way awaited method still behaves asynchronously and we get our result when it is finished executing.
Then you don't need .Result at all. Just use what await returned. BTW, your code won't compile because t is NOT a task. It's the result of the Task, ie a string
@Eqra Check the edited answer. It is more elaborated now. Code will execute asynchronously. That is what async/await do. I just meant that after await, you can access t.result which is asynchronous way of retrieving values from async methods. But definitely you need to await first and then use t.Result.
|
-4

If you await it, it will be executed asynchronously. If you do t.Result it will be executed synchronously.

It really depends on your needs to execute it sync or async.

Here we go with the supposed expert programmers downvote rush.

Try to remove await, put .Result and you will get this:

"This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread."

4 Comments

I want to do it asynchronously but want a final message too when my asynchronous method will complete its processing.
@Eqra that's what await is for
.Result won't execute anything synchronously. The task will still run asynchronously. The calling thread though will block until the tast returns
Indeed, calling .Result doesn't change how the task executes at all (unless the task's execution needs to execute more code on the calling thread, of course, causing deadlock). It changes how the code calling .Result behaves.

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.