Wednesday, March 4, 2015

Asynchronous Programming With Async Await


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

namespace Asynchronous_Programming_With_Async_Await
{

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

        //===========================Demonstrating how long running method locked up
        private void btnRunLongRunningNormallyLockUI_Click(object sender, EventArgs e)
        {
            DisplaySumOfRandomValues();
        }
        //Define a long running method
        private void DisplaySumOfRandomValues()
        {
            double sum = 0;

            for (int i = 1; i < 1000; i++)
            {
                sum += rand.Next(1000000, 2000000);
                Thread.Sleep(1); //simulating long running method.
            }
            richTextBox1.Text = "From long running method: \n" + "sum = " + sum;
        }

        //==================================================================
        // Use async await to make UI responsive
        //reDefine the method to include a Task
        //Divide the method in two sections.
        //Section 1: contain a Task to run the long running
        //code portion
        //Section 2 contains the display code (or any code that does not lock up the ui)

        private async void btnRunNotLookedUpUI_Click(object sender, EventArgs e)
        {
           await DisplaySumOfRandomValuesAsync();
        }

        private async Task DisplaySumOfRandomValuesAsync() //Noticed "Async" keyword is added
            //***To follow the naming convention the method name has to be ended with "*Async"
        {
            double sum = 0;
            //first section
            //run the for loop (long running portion) in a task
            Task task = Task.Factory.StartNew(() =>
                {
                    for (int i = 1; i < 100; i++)
                    {
                        sum += rand.Next(1000000, 2000000);
                        Thread.Sleep(1); //simulating long running method.
                    }
                });

            //cause this method to suspend itself and return to the caller
            //this spot is bookmarked
            //When the task completes, the compiler resumes execution of this method.
            await task;
            //second section
            richTextBox1.Text = "From long running method: \n" + "sum = " + sum;
        }
        //==================================================================
        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

No comments:

Post a Comment