Solved

Task.Run Method

Posted on 2015-02-19
2
133 Views
Last Modified: 2015-02-21
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
Comment
Question by:IzzyTwinkly
2 Comments
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 40620467
(() => 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
 
LVL 23

Expert Comment

by:Michael74
ID: 40620473
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question