Friday, March 6, 2015

AsyncAwait Lab Project



using System.Windows.Forms;
using System.Threading; //Added



namespace ZauBawk_AsyncAwait_Lab
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRunFibfromNormalButton_Click(object sender, EventArgs e)
        {
            ulong userInput = 42;
            ulong result = Fibonacci(userInput);
            richTextBox1.AppendText("fib(42) = " + result.ToString());
        }

        private void btnRunNormalPrime_Click(object sender, EventArgs e)
        {
            double sum = GetSumOfPrimeNumbers();
            richTextBox1.AppendText("Total of prime number: " + sum);
        }
        //========================================
        private async void btnRunbothmethods_Click(object sender, EventArgs e)
        {
         
            await GetFibonacciValueOf42Async();
            await GetSumOfPrimeNumbersAsync();
        }
     
       
        //method to sum prime numbers
        private async Task GetSumOfPrimeNumbersAsync()
        {
            double sum = 0;
            //first section
            //run the for loop (long running portion) in a task
            Task task = Task.Factory.StartNew(() =>
            {
                for (int i = 1000000; i < 2000000; i++)
                {
                    if(isPrime(i))
                    {
                        sum += i;
                        //Thread.Sleep(1); //simulating long running method.
                    }
                }
            });
            await task;
            //second section
            //richTextBox1.Text = "From long running method: \n" + "sum = " + sum;
            richTextBox1.AppendText("Total of Prime Numbers: " + sum + "\n");
        }

        //=================================================================================
        //method to sum prime numbers
        private async Task GetFibonacciValueOf42Async()
        {

            ulong value = 42;
            ulong result = 0;
            Task task1 = Task.Factory.StartNew(() =>
            {
                result = Fibonacci(value);
                //richTextBox1.AppendText("fib(42) = " + result.ToString());

            });
            await task1;
            richTextBox1.AppendText("Fibonacci value of 42 is: " + result + "\n");
        }

        //simple method to return a Fibonacci value
        private ulong Fibonacci(ulong num)
        {
            if (num == 0) return 0;
            if (num == 1) return 1;
            return Fibonacci(num - 1) + Fibonacci(num - 2);
        }

        ////method to sum prime numbers
        private double GetSumOfPrimeNumbers()
        {
            double sum = 0;
            for (int i = 1000000; i <= 2000000; i++)
            {
                if (isPrime(i))
                {
                    sum += i;
                }
            }
            return sum;
        }

        //method to get prime bumber
        public static bool isPrime(int number)
        {
            double boundary = Math.Floor(Math.Sqrt(number));

            if (number == 1) return false;
            if (number == 2) return true;

            for (int i = 2; i <= boundary; ++i)
            {
                if (number % i == 0) return false;
            }
            return true;
        }

        private void btnTestIfUIisLookedUp_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test if UI has been locked up");
        }

    }
}
//Lab: Due Tomorrow
//Define two long running methods
//First method Displays the fibonacci of some value (like 42)
//Second method Displays the sum of Prime numbers (between 1000000 and 2000000)

//1. Run both method normally from different buttons
//2. run both method asynchronously using async await from a single button
//3. Add a button to test whether the UI is or is not locked
////www.dotnetperls.com/async
///www.codeproject.com/Tips/591586/Asynchronous-Programming-in-Csharp-using-async
////stackoverflow.com/questions/16728338/how-can-i-run-both-of-these-methods-at-the-same-time-in-net-4-5
///msdn.microsoft.com/en-us/library/vstudio/hh191443(v=vs.110).aspx#BKMK_HowtoWriteanAsyncMethod
///blog.stephencleary.com/2012/02/async-and-await.html

No comments:

Post a Comment