O'Reilly logo

C# Multithreaded and Parallel Programming by Rodney Ringler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Tasks with return values

Now, we will take a look at how to start a task that returns a value. In most cases, if we care about the result of a task or if a task does some work to be consumed by the rest of the program, then we will want the task to return some values for us to use. We will demonstrate this by developing a simple console application that starts three tasks and then prints the return values of these three tasks.

How to do it

We will start by creating a new console application using Visual Studio 2013. We will name our application, TaskExampleWithReturnValues.

Next, place the following code in the Program class of Program.cs:

class Program { static void Main(string[] args) { //Create the 3 Tasks. Task<String> t1 = new Task<String>(() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required