Friday, February 27, 2015

Parent Child Task - Lab


using System.Windows.Forms;

namespace ZauB_ParentChildTask_Lab_2_18
{

    public partial class Form1 : Form
    {
        Random rand = new Random();
        public Form1()
        {
            InitializeComponent();
        }
        //=====================Method to set Text=======================
        //1. define a delegate with same signature as the crosss-threading handling
        // method (the SetText method)
        private delegate void RichTextBoxHandler(string s, RichTextBox rtb);
        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
                rtb.ScrollToCaret();
            }
        }

        private void btnGetTotalValuesByParentTask_Click(object sender, EventArgs e)
        {
            Task parentTask = Task.Factory.StartNew(() =>
            {
                SetText("Parent task started execution", richTextBox1);
                //Start a task within the parentTask. Such Task is therefore a nested task
                Task<long> evenSumT = Task<long>.Factory.StartNew(() =>
                {
                    int counter = 0;
                    long evenSum = 0;

                    do
                    {
                        int num = rand.Next();
                        if (num % 2 == 0)
                        {
                            evenSum += num;
                            counter++;
                        }
                    } while (counter < 10000000);
                    return evenSum;
                }, TaskCreationOptions.AttachedToParent);

                long evensum = evenSumT.Result;
                SetText("Total of even random numbers is: " + evensum.ToString(), richTextBox1);

                Task<long> oddSumT = Task<long>.Factory.StartNew(() =>
                {
                    int counter = 0;
                    long oddSum = 0;
                    do
                    {
                        int num = rand.Next();
                        if (num % 2 != 0)
                        {
                            oddSum += num;
                            counter++;
                        }
                    } while (counter < 10000000);
                    return oddSum;
                   
                }, TaskCreationOptions.AttachedToParent);
                long oddsum = oddSumT.Result;
                SetText("Total of odd random numbers is: " + oddsum.ToString(), richTextBox1);


            });//This is added for child task.
            parentTask.ContinueWith(task =>
            {
                //At this point in time the parenttask has completed its work
                SetText("Parent task completing.", richTextBox1);
            });
        }
    }
}
//Lab Assignment
//Define 2 methods similar to the ones we did before 
//GetTotalofOddRandomValues and GetTotalOfEvenRandomValues (may add them as lambda expression)
//Run each method in a separate tasks (task1 and task2)
//Both tasks should run inside a parent task
//The parent task is to get the return values of both task1 and task2, add them and
//display the result
//
//P.S: the methods should not display anyting. you may add a longer loop to makes each
//method run for few seconds
//http://dotnetcodr.com/2014/01/01/5-ways-to-start-a-task-in-net-c/

No comments:

Post a Comment