Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async-Await expression returns wrong result [closed]

I have a problem with async-await expression which returns wrong result.

private Task<int> A
{
    get
    {
        return TaskEx.RunEx<int>(async () =>
        {
            Thread.Sleep(10000);
            return 2;
        });
    }
}

private Task<int> B
{
    get
    {
        return TaskEx.RunEx<int>(async () =>
        {
            Thread.Sleep(4000);
            return 4;
        });
    }
}

private string SumAll(int a, int b)
{
    return (a + b).ToString();
}

Now, when I want to sum properties A and B by launching SumAll method I get the result 4 where I should get 6. Below you can find a code which does not work.

private async void Sum_Click(object sender, RoutedEventArgs e)
{
    this.Result.Text = this.SumAll(await A, await B);
}

When, I do the same example with the method below I got the right result.

private async void Sum_Click(object sender, RoutedEventArgs e)
{
    var a = await A;
    var b = await B;
    this.Result.Text = this.SumAll(a, b);
}

BTW. I know that the best way would be to use WhenAll method, but I am learing. Thank you for your answers

like image 945
Jarek Avatar asked Dec 10 '25 19:12

Jarek


1 Answers

Yes, this is a known bug with the CTP (my blog; Lucian Wischik's blog). It'll be fixed in the full release.

like image 193
Jon Skeet Avatar answered Dec 13 '25 09:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!