ProfessionalCSharp/ProfessionalCSharp2021

Should we call Wait method when we call Result property in Tasks? C# Parallel Programming

Closed this issue · 0 comments

Discussed in #169

Get rid of a few code lines here. Why did I retrieve the result the first time? Remove the first WriteLine and the Remove.

Originally posted by ShervanN March 13, 2023
Hi,

In chapter 17 Parallel Programming, Page 541, You wrote:

    public static void TaskWithResultDemo()
    {
        Task<(int Result, int Remainder)> t1 = new(TaskWithResult, (8, 3));
        t1.Start();
        Console.WriteLine(t1.Result);
        t1.Wait();
        Console.WriteLine($"result from task: {t1.Result.Result} {t1.Result.Remainder}");
    }