Task Returing Value C# with Example



Task Returing Value C# with Example

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

namespace CSharpMultiThreading
{
	class DemoTask 
	{
		// A trivial method that returns a result and takes no arguments.
		static bool MyTask() 
		{
			return true;
		}
		
		// This method returns the summation of a positive integer which is passed to it.
		static int SumIt(object v) 
		{
			int x = (int) v;
			int sum = 0;
			for(; x > 0; x--)
			sum += x;
			return sum;
		}
		
		static void Main() 
		{
			Console.WriteLine("Main thread starting.");

			// Construct the first task.
			Task tsk = Task.Factory.StartNew(MyTask);
			Console.WriteLine("After running MyTask. The result is " + tsk.Result);

			// Construct the second task.
			Task tsk2 = Task.Factory.StartNew(SumIt, 3);
			Console.WriteLine("After running SumIt. The result is " + tsk2.Result);
			
			tsk.Dispose();
			tsk2.Dispose();
			Console.WriteLine("Main thread ending.");
		}
	}
} 

0 Comment's

Comment Form

Submit Comment