Solved

Task.Run Method

Posted on 2015-02-19
2
124 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now