none
Async/Await: How to capture return value when not using await keyword RRS feed

  • Question

  • see my sample code first

    private  void button1_Click(object sender, EventArgs e)
            {
                Task<int> result = MyMethodAsync();
                MessageBox.Show(result.ToString());
                int x = 0;
            }
    
            public async Task<int> MyMethodAsync()
            {
                Task<int> longRunningTask = LongRunningOperationAsync();
                // independent work which doesn't need the result of LongRunningOperationAsync can be done here
    
                //and now we call await on the task 
                int result = await longRunningTask;
                //use the result 
                //Console.WriteLine(result);
                return result;
            }
    
            public async Task<int> LongRunningOperationAsync() // assume we return an int from this long running operation 
            {
                await Task.Delay(2000); // 1 second delay
                return 1;
            }

    just see Task<int> result = MyMethodAsync(); i am calling a asycn function without await keyword. so when MyMethodAsync() complete and return value then i am not being able to capture that value.

    i know if i use await along with this line int result = await MyMethodAsync() then i can hold the return value.

    so i am interested to know how could i hold return value when not using await keyword at the time of calling async function MyMethodAsync().

    without await keyword can i hold the return value which asycn function will return ?

    please guide me.

    Sunday, December 23, 2018 5:58 PM

Answers

  • First of all your button click event code should be :

    private async void button1_Click(object sender, EventArgs e)
    {
         int result = await MyMethodAsync();
         MessageBox.Show(result.ToString());
    }

    await will unwrap the result of the Task<int> and return you the int back.

    You can get the result too by not making it async like following:

    private void button1_Click(object sender, EventArgs e)
    {
         int result = MyMethodAsync().Result;
         MessageBox.Show(result.ToString());
    }

    You can also use The Result  property but i don't recommend that, be cautious calling it inside an async caller method that can lead to deadlocks, but just for knowledge you can do the following but this will block the caller method means the caller method will no longer be working asynchronously:

    public int MyMethodAsync()
    {
        Task<int> longRunningTask = LongRunningOperationAsync();
        // independent work which doesn't need the result of 
           LongRunningOperationAsync can be done here
    
         
         int result = longRunningTask.Result;
         //use the result 
         //Console.WriteLine(result);
         return result;
     }

    But the way you are doing in the original question is the best practice. We should not directly access the Result inside an async method.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites




    • Edited by Ehsan Sajjad Monday, December 24, 2018 4:56 AM
    • Marked as answer by Sudip_inn Saturday, January 12, 2019 5:27 PM
    Monday, December 24, 2018 4:51 AM
  • Hi Sudip_inn,

    Thank you for posting here.

    First, I wanna to show my test. We could get the result if we do not use the await. When you debug the code step by step using F11, in the end, you could see that the value "1" is returned. But the code run the MessageBox first. Hence you do not see the value "1".

    As a supplement, Winform has UI thread. If we use task.Wait() or task.Result, it will a blocking call in UI thread which causes the deadlock.

    The usage is the best way to get the value.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 24, 2018 7:15 AM
    Moderator

All replies

  • First of all your button click event code should be :

    private async void button1_Click(object sender, EventArgs e)
    {
         int result = await MyMethodAsync();
         MessageBox.Show(result.ToString());
    }

    await will unwrap the result of the Task<int> and return you the int back.

    You can get the result too by not making it async like following:

    private void button1_Click(object sender, EventArgs e)
    {
         int result = MyMethodAsync().Result;
         MessageBox.Show(result.ToString());
    }

    You can also use The Result  property but i don't recommend that, be cautious calling it inside an async caller method that can lead to deadlocks, but just for knowledge you can do the following but this will block the caller method means the caller method will no longer be working asynchronously:

    public int MyMethodAsync()
    {
        Task<int> longRunningTask = LongRunningOperationAsync();
        // independent work which doesn't need the result of 
           LongRunningOperationAsync can be done here
    
         
         int result = longRunningTask.Result;
         //use the result 
         //Console.WriteLine(result);
         return result;
     }

    But the way you are doing in the original question is the best practice. We should not directly access the Result inside an async method.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites




    • Edited by Ehsan Sajjad Monday, December 24, 2018 4:56 AM
    • Marked as answer by Sudip_inn Saturday, January 12, 2019 5:27 PM
    Monday, December 24, 2018 4:51 AM
  • Hi Sudip_inn,

    Thank you for posting here.

    First, I wanna to show my test. We could get the result if we do not use the await. When you debug the code step by step using F11, in the end, you could see that the value "1" is returned. But the code run the MessageBox first. Hence you do not see the value "1".

    As a supplement, Winform has UI thread. If we use task.Wait() or task.Result, it will a blocking call in UI thread which causes the deadlock.

    The usage is the best way to get the value.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 24, 2018 7:15 AM
    Moderator