• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Task.Run Method

Hi, I am really confused by the first parameter for Task.Run() method.
I am reading Illustrated C# and it has the following async method.

public static async Task DoWorkAsync()
 {
            await Task.Run(() => Console.WriteLine(5.ToString())); // 1
            Console.WriteLine((await Task.Run(()=>6)).ToString()); //  2
            await Task.Run(()=> Task.Run(()=>Console.WriteLine(7.ToString()))); //3
            int value = await Task.Run(() => Task.Run(() => 8));  //4
 }

For //1,  it says that a method that takes no parameters and returns no value.
When we look at this part,
(() => Console.WriteLine(5.ToString()))
I understand that it has no parameter, but doesn't it return "5"? Why does it say that the method returns no value?
Isn't it like returning an object of type T as //2?

For //2, the book says that a method that takes no parameters and returns an object of type T.
The part (()=>6)) shows that it has no parameter and returns 6.

For //3, the book says the the method that has no parameters and return a simple Task object.
(()=> Task.Run(()=>Console.WriteLine(7.ToString())))
According to //1, the part, (()=>Console.WriteLine(7.ToString()), returns no value. but then I got lost. can anybody break down this so that I can understand how this returns "a simple Task object"?

for //4, the book says that a method that takes no parameters and returns an object of type Task<T>
can you tell me how this returns Task<t> object?
0
IzzyTwinkly
Asked:
IzzyTwinkly
1 Solution
 
ambienceCommented:
(() => Console.WriteLine(5.ToString()))
I understand that it has no parameter, but doesn't it return "5"? Why does it say that the method returns no value?
Isn't it like returning an object of type T as //2?

Console.Write has void return type, which means it does not return anything. If you see a 5 printed, thats because of the Console.Writeline. Here this overload of Task.Run is used

public static Task Run(
	Action action
)

Open in new window



For //3, the book says the the method that has no parameters and return a simple Task object.
(()=> Task.Run(()=>Console.WriteLine(7.ToString())))
According to //1, the part, (()=>Console.WriteLine(7.ToString()), returns no value. but then I got lost. can anybody break down this so that I can understand how this returns "a simple Task object"?

Open in new window


await Task.Run(()=> Task.Run(()=>Console.WriteLine(7.ToString())));

can be divided into

Task taskInner = Task.Run(  ()=>Console.WriteLine(7.ToString(); )
Task<Func<Task>> taskOuter = Task.Run ( ()=> taskInner ) );

await taskOuter;

Open in new window


The inner Task,Run returns a Task object (which is what the book talks about). Why is it returning a Task and not Task<T>? The inner task uses the overloaded that takes Action and returns a Task. Same as for 1.

await Task.Run(() => Task.Run(() => 8));
for //4, the book says that a method that takes no parameters and returns an object of type Task<T>
can you tell me how this returns Task<t> object? 

Open in new window


This can be broken down the same way, except for one difference in the type of Inner Task, which is Task<int> now (T here is int).

Task<int> taskInner = Task.Run(  ()=> 8 );
Task<Func<Task>> taskOuter = Task.Run ( ()=> taskInner ) );

int value = await taskOuter;

Open in new window


Hope this helps.
0
 
Michael FowlerSolutions ConsultantCommented:
5.ToString() Just converts the integer 5 to string value. It is not the return value.

In the examples it does not appear that the return object is being read rather a integer value is being output to the console so you can see the program run.

I would suggest that put this code into VS and step though the code to have a look at what is happening in more detail
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now