Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Task.Run Method

Posted on 2015-02-19
2
Medium Priority
?
160 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 2000 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
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…
Suggested Courses

715 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