Sunday, February 8, 2015

Asynchronous Methods Lab Assignment


namespace Zau_AsynchronousMethos_Lab_2_4_2015
{

    //A class object to use with Function delegate
    public class UserInput
    {
        public int V1 { get; set; }
        public int V2 { get; set; }
    }
}
//==================================================
using System.Windows.Forms;

namespace Zau_AsynchronousMethos_Lab_2_4_2015
{
    public partial class Form1 : Form
    {
        Random rand = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRunAsynchronously_Click(object sender, EventArgs e)
        {
            //Func<> delegate value return method
            Func<double> function = GetTotalOfLog;

            //AsyncCallack delegate to reference the method
            AsyncCallback callback = CallbackOfGetTotalOfLog;
            function.BeginInvoke(callback, function);
        }

        //method to get sum of log of generate values
        private double GetTotalOfLog()
        {
            int num;
            double sumOfLog = 0;
            for (int i = 0; i < 9000000; i++)
            {
                num = rand.Next();
                sumOfLog =+ Math.Log(num);
            }
            return sumOfLog;
        }

        //Callback method for the asynchronous GetTotalOfLog()
        private void CallbackOfGetTotalOfLog(IAsyncResult ar)
        {
            Func<double> function = (Func<double>)ar.AsyncState;
            double sumOfLog = function.EndInvoke(ar);
            SetText("Sum of log of num: " + sumOfLog, richTextBox1);
        }

        //handling method (the SetText method)
        private delegate void RichTextBoxHandler(string s, RichTextBox rtb);
        //method to allow other threads
        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();
            }
        }

        //=============================================================
        //method to get sum of log of generate values
        private double GetAverageOfAddedValue(Object data)
        {
            UserInput uI = (UserInput)data;
            double sum = 0;
            int counter = 0;
            int startNum = uI.V1;
            int lastNum = uI.V2;

            for (int i = startNum; i < lastNum; i++)
            {
                sum = i + (i * 0.01);
                counter++;
            }
            return sum / counter;
        }


        private void btnGetAvgOfLogSum_Click(object sender, EventArgs e)
        {
            int value1;
            int.TryParse(txtStartNum.Text, out value1);
            int value2;
            int.TryParse(txtEndNum.Text, out value2);
            UserInput data;
            if (value1 < value2)
            {
                data = new UserInput { V1 = value1, V2 = value2 };
            }
            else
            {
                data = new UserInput { V1 = value2, V2 = value1 };
            }
            Func<Object, double> function = GetAverageOfAddedValue;

            AsyncCallback callback = CallbackOfGetAverageOfAddedValue;
            function.BeginInvoke(data, callback, function);
        }

        private void CallbackOfGetAverageOfAddedValue(IAsyncResult ar)
        {  
            Func<Object, double> function = (Func<Object, double>)ar.AsyncState;    
            double average = function.EndInvoke(ar);
            SetText("Average of Total added value: " + average, richTextBox2);
        }

    }
}
//Lab Assignment: New Project || Due Wednesday 2-4; 
//Define a method that takes no parameters and returns a double 
//The method is to run asynchronously and computes the sum of Log of 
//all the random values generated. (generate few millions)
// sum += Math.log(num)
//Get and display the result thru its callback method
//====================================================
//Define a method that takes 2 integer values
//Add all the values between the 2 parameters incrementing by 0.01
//run the method asynchronously
//the method is to return the average value
//get and display its return value from the callback

No comments:

Post a Comment