Parallel Programming- Invoke Method C# with Example



Parallel Programming- Invoke Method C# with Example

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

namespace CSharpMultiThreading
{
	class DemoCancelTask 
	{
		// A method to be run as a task.
		static void MyTask() 
		{
			Console.WriteLine("MyTask() starting");
			for(int count = 0; count < 5; count++) 
			{
				Thread.Sleep(500);
				Console.WriteLine("In MyTask(), count is " + count );
			}
			Console.WriteLine("MyTask terminating");
		}

		// A method to be run as a task.
		static void YourTask() 
		{
			Console.WriteLine("YourTask() starting");
			for(int count = 0; count < 5; count++) 
			{
				Thread.Sleep(500);
				Console.WriteLine("In YourTask(), count is " + count );
			}
			Console.WriteLine("YourTask terminating");
		}
		
		static void Main() 
		{
			Console.WriteLine("Main thread starting.");
			Parallel.Invoke(MyTask, YourTask);
			Console.WriteLine("Main thread ending.");
		}
	}
} 

0 Comment's

Comment Form