A day with .Net

My day to day experince in .net

Getting result from A task

Posted by vivekcek on August 19, 2012

Some times your task may return some value. In this post i will show how to get an integer result from a task you created.

For that you have to use the generic version of task Task.

The Result property of Task will give result of type T.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GetResult1
{
    class Program
    {
        static void Main(string[] args)
        {
            Task<int> t1 = new Task<int>(() => Add());
            t1.Start();
            Console.WriteLine("Result: {0}", t1.Result);
            Console.ReadLine();
        }
        static int Add()
        {
            return 1 + 2;
        }
    }
}


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s