Saturday, February 28, 2015

Parallel Invoke Method (In Console)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Parallel_InvokeMethod_2
{

    class Program
    {
        //Class Exercise #2 Tuesday,24,2015
        //Parallel Invoke Method
        //Main Method

        static void Main(string[] args)
        {
            Console.WriteLine("Press Enter to Display");
            Console.ReadKey();
            //if you want to create an action...this is one option to display
            Action action1 = DisplayTemperatureTable;


            //Parallel.Invoke(action1);//you can listed by action
            //or
            //you can listed by the lambda expression
            Parallel.Invoke(
                action1,
                () =>
                {
                    for (int i = 0; i < 100; i++)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("taskId={0} Int= {1}", Task.CurrentId.ToString(), i);
                    }
                },
                 () =>
                 {
                     for (int i = 0; i < 100; i++)
                     {
                         Console.ForegroundColor = ConsoleColor.Blue;
                         Console.WriteLine("taskId= {0} Int= {1}", Task.CurrentId.ToString(), i);
                     }
                 });
            Console.WriteLine("Press enter to continue");
            Console.ReadKey();
        }

        //method to display temperature table
        static void DisplayTemperatureTable()
        {
            for (int F = 0; F <= 212; F++)// F means Farenheit
            {
                int C = (F - 32) * 5 / 9;// C = Celsius
                Console.WriteLine("TaskId={0} F={1} then C= {2}", Task.CurrentId.ToString(), F, C);
            }
        }
    }
}
///Lab assigment:
///Windows application
///Use Parallel.Invoke method to run 3 actions/methods in parallel
///each reading and displaying a separate text file.


No comments:

Post a Comment