Understanding CallBack function on Thread.


Understading Callback method wher a function is executed not on the main thread but on a child thread that it created .  Similary there are different ways of executing function created on the child thread.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Memory_understanding
{
    public delegate void sumofnumberscallback(int sumofnumber);
    class A
    {
        int target;
        public A(int num)
        {
            this.target= num;
        }
        public void printNumber(int n)
        {
           
            for (int i = 0; i < n; i++)
            {
                Thread.Sleep(1000);
                Console.WriteLine(i);
            }
        }

        public void printNumber_2()
        {

            for (int i = 0; i < target; i++)
            {
                Thread.Sleep(1000);
                Console.WriteLine(i);
            }
        }


    }

    class computesum
    {
        int sum;
        sumofnumberscallback callback;

        public computesum(int num,sumofnumberscallback callbackmethod)
        {
            this.sum = num;
            this.callback = callbackmethod;

        }
            //3) The Child thread computes the sum of the numbers and then returns the sum of numbers to the
            //    main thread using a call back functions
        public void sum_()
        {
            int count_sum = 0;
            for (int i = 0; i <= sum; i++)
            {
                count_sum = count_sum + i;
            }

            if (callback != null)
                callback(count_sum);
        }

    }
    class Program
    {
        //4) The call back method then prints the sum of the numbers
        public static void PrintSum(int sum)
        {
            Console.WriteLine(sum);
        }

        public static void Main()
        {
            //Retrieveing Data from a thread function with using call back method using delegate

            //1) Main thread will retrieves the target number from the user
            //2) Main thread created a child thread and pass the target number to the child thread
            //3) The Child thread computes the sum of the numbers and then returns the sum of numbers to the
            //    main thread using a call back functions
            //4) The call back method then prints the sum of the numbers

            Console.WriteLine("Enter the value ");
            int t = Convert.ToInt32(Console.ReadLine());       //1) Main thread will retrieves the target number from the user

            sumofnumberscallback callback = new sumofnumberscallback(PrintSum); //4) The call back method then prints the sum of the numbers

            computesum c = new computesum(t, callback);
            Thread t5 = new Thread(new ThreadStart(c.sum_));  //2) Main thread created a child thread and pass the target number to the child thread
            t5.Start();




            A a = new A(t);

            //one way of writing a invoking a function on a particular thread with a parameter passed
            //using lamda expression

            Thread t1 = new Thread(() => a.printNumber(t));
            t1.Start();

            //another way of invoking a function on a particular thread with a parameter passed
            //using delegate

            Thread t2 = new Thread(delegate() { a.printNumber(t); });
            t2.Start();
           

            //understanding the passing of parameter data to a thread function in a type safe manner wth thread start delegate

            Thread t3 =new Thread(new ThreadStart(a.printNumber_2));
            t3.Start();
            Console.ReadLine();

            //understanding the passing of parameter data to a thread function in a type safe manner wthout using thread start delegate
            // ther compiler will do the implicit conversion of the threadstart delegate as shown below.

            Thread t4 = new Thread(a.printNumber_2);
            t4.Start();
            Console.ReadLine();

        }
     
    }
}

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6