A day with .Net

My day to day experince in .net

Creating and starting task

Posted by vivekcek on August 19, 2012


In this post i mentioned the various method to create and start a task
.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StartTask
{
    class Program
    {
        static void Main(string[] args)
        {
            //Using Action
            Task t1 = new Task(new Action(SayHello));
            t1.Start();

            //Using delegate
            Task t2 = new Task(delegate { SayHello(); });
            t2.Start();

            //Lambda Expression
            Task t3 = new Task(() => SayHello());
            t3.Start();

            //Anonymous Metod
            Task t4 = new Task(() => { Console.WriteLine("Hello"); });
            t4.Start();


            ////////Task.Factory/////////////

            //No need to call any start method
            Task.Factory.StartNew(() => SayHello());

            Console.ReadLine();
        }
        static void SayHello()
        {
            Console.WriteLine("Hello");
        }
    }
}
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