Thursday, February 12, 2015

Introduction To Task



namespace Introduction_To_Task
{

    //See TPL: Task Parallel Library: System.Threading.Tasks namespace
    //https://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx
    //https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx

    public partial class Form1 : Form
    {
        Random rand = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        //define 2 methods 
        private void DisplayOddRandomNumbers()
        {
            //Display 1,000 odd random
            int counter = 0;
            do
            {
                int num = rand.Next();
                if (num % 2 != 0)
                {
                    //richTextBox1.AppendText(counter + "    " + num + "\n");
                    SetText(counter + "  " + num, richTextBox1);

                    //scroll down automaically
                    //rtb.ScrollToCaret();
                    counter++;
                }
            } while (counter < 1000);
        }

        //=============================================================
        //handling method (the SetText method)
        private delegate void RichTextBoxHandler(string s, RichTextBox rtb);

        //method to allow other threads to access the UI RichTextBox
        //this method should accept the string that you want to display in the UI
        private void SetText(string s, RichTextBox rtb)
        {
            if (rtb.InvokeRequired)
            {
                //create a delegate object
                RichTextBoxHandler handler = SetText;
                Invoke(handler, s, rtb);
            }
            else
            {
                rtb.AppendText(s + "\n");
                //scroll down automaically
                rtb.ScrollToCaret();
            }
        }

        private void btnStartTasks_Click(object sender, EventArgs e)
        {
            //Create a Task and start it
            //3 options are provided
            //1. Options 1: Create a Task object, and then apply the Start method
            Action action1 = DisplayOddRandomNumbers;
           // Task task1 = new Task(action1);
            //or
            Task task1 = new Task(DisplayOddRandomNumbers);
            task1.Start();
            //2. Option 2: use the Property Factory of the Task class.
            //The Factory property is defined as:
            //public static TaskFactory Factory { get; }
            //The TaskFactory class defines the method: StartNew
            //The StartNew method creates a tasks and starts it as well.
            //It is the preferred way to option 1
            //Ref: ttps://msdn.microsoft.com/en-us/library/dd321439(v=vs.110).aspx

            Task task2 = Task.Factory.StartNew(DisplayEvenRandomNumbers);

            //3. Option 3: 
           // Task task3 = Task.Run(() => DisplayOddRandomNumbers());
            //or
            //Task task3 = Task.Run(new Action(DisplayOddRandomNumbers));

        }

        private void DisplayEvenRandomNumbers()
        {
            //Display 1,000 odd random
            int counter = 0;
            do
            {
                int num = rand.Next();
                if (num % 2 == 0)
                {
                    // richTextBox2.AppendText(counter + "    " + num + "\n");
                    SetText(counter + "  " + num, richTextBox2);
                    //scroll down automaically
                    //rtb.ScrollToCaret();
                    counter++;
                }
            } while (counter < 1000);
        }
    }
}
//=============================================================
//Keywords: task factory, 
//Lab Assignment: Due Tuesday Feb 10.
//Use the following Task constructor
//public Task(Action<Object> action, Object state)
//This constructor allows you to pass a paremeter of type Object to the method
//Write a method that takes a parameter (like the number of random odds to display
//and run the method in a task using The Task constructor and the TaskFactory.

No comments:

Post a Comment