Solved

Task.Run Method

Posted on 2015-02-19
2
144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Michael Fowler
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 9 hours left to enroll

617 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