Task As Lambda Expression C# with Example



Task As Lambda Expression C# with Example

 using System;
using System.Threading;
using System.Threading.Tasks;

namespace CSharpMultiThreading
{
	class DemoTask 
	{
		static void Main() 
		{
			Console.WriteLine("Main thread starting.");

			// Construct and Run a task.
			Task tsk = Task.Factory.StartNew(
			//Lambda Expression of MyTask
			() =>{
				Console.WriteLine("MyTask() starting");
				for(int count = 0; count < 10; count++) 
				{
					Thread.Sleep(500);
					Console.WriteLine("In MyTask(), count is " + count);
				}
				Console.WriteLine("MyTask terminating");
			}
		);

			// Keep Main() alive until MyTask() finishes.
			tsk.Wait();
			
			Console.WriteLine("Main thread ending.");
		}
	}
} 

0 Comment's

Comment Form

Submit Comment