Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog

Loading

Sunday, June 20, 2010

Retrieving data from threads C# example




To understand how to retrieve data from threads, you need to have complete understanding of how delegates work. If you are unsure about how delegates works, you can check out my previous post on delegates on the link below:
Delegates tutorial
The above post provides you video tutorial on the same topic, so do check it out.
Step-1:

Now, to retrieve data from a thread, you need to create a method that accepts return results as a parameter and to that we need to create a delegate for the method.
For example, Imagine a thread is passing salary of an employee to a method called displaySalary. The method definition of displaySalary is as follows:

public static void displaySalary(int salary)
        {
            Console.WriteLine("Renewed salary of the employee is : "+salary );
            Console.WriteLine("Press any key to clear the screen: ");
            Console.ReadKey();
            Console.Clear();
            flag = 0;
           
        }
As i said, to the above method a thread is going to pass value "salary". But for a thread to pass value to this method, we need to create a delegate for the above method.
This is how you create a delegate with exact same signature as the method displaySalary.

public delegate void Mydelegate(int value);

Remember the above statement doesn't link your delegate to the specified method. To link your method to delegate, you must create an obect of the delegate as follows:

Mydelegate PassValDelegate = new Mydelegate(displaySalary);

Great, now you have a delegate object linking to your method. Now for the next step of passing value from a thread.




Step-2:

Imagine i have a method called setSalary, whose definition is as follows:

 public void setSalary()
        {
            Console.Clear();
            Console.WriteLine("Please provide the hike in salary and press enter:");
            int hike = Convert.ToInt32(Console.ReadLine().ToString());
            int Reviewedsalary = Convert.ToInt32(salary) + hike;
          

        }

Create a thread to execute above method in background as follows:

Thread t1 = new Thread(new ThreadStart(setSalary));
  t1.Start();

If i want to pass Reviewedsalary value to the method displaySalary, then i need to pass it using the delegate object created above. So now our method setSalary would look like this:

 public void setSalary()
        {
            Console.Clear();
            Console.WriteLine("Please provide the hike in salary and press enter:");
            int hike = Convert.ToInt32(Console.ReadLine().ToString());
            int Reviewedsalary = Convert.ToInt32(salary) + hike;
           //you can pass value to displaySalary using the delegate object as follows
           PassValDelegate(Reviewedsalary);


        }

This is little complex to understand.However check out my video, i will try to explain few things which can't be explained in writing.

Now for the working example of the above tutorial. Please check out the delegates examples by following the link i have provided you in the beginning of the post so that understanding this example will be easier.

Please start  a console project and name it as PassValBetThreads. To this project add a class and name it as employee. My class employee has 4 methods which display information about employee and 2 methods which set employee information. Go ahead and write the methods as follows:

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

namespace PassValBetThreads
{
    public delegate void Mydelegate(int value);
    public class Employee
    {
        public string name;
        public string designation;
        public string department;
        public string salary;
        public Mydelegate PassValDelegate;

       
        public  void setEmployee(string _setname, string _setdesignation, string _setdepartment, string _setsalary)
        {
            name = _setname;
            designation = _setdesignation;
            department = _setdepartment;
            salary = _setsalary;
        }

        public void setSalary()
        {
            Console.Clear();
            Console.WriteLine("Please provide the hike in salary and press enter:");
            int hike = Convert.ToInt32(Console.ReadLine().ToString());
            int Reviewedsalary = Convert.ToInt32(salary) + hike;
            PassValDelegate(Reviewedsalary);


        }

        public void displayname()
        {
            Console.WriteLine("Name of the employee is : "+ name);
        }
        public void displaydesignation()
        {
            Console.WriteLine("Designation of the employee is : " + designation);
        }

        public void displaydepartment()
        {
            Console.WriteLine("Department of the employee is : " + department);
        }

        public void displaysalary()
        {
            Console.WriteLine("Salary of the employee is : " + salary + " Dollars");
        }
    }
}

Now go to program.cs file of your console application and write the methods as follows:

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

namespace PassValBetThreads
{
  
    class Program
    {
         public static int flag = 0;
        public delegate void MethodPointer();
      
        static void Main(string[] args)
        {
           
            int choice = 0;
            Employee Sam = new Employee();
            Sam.setEmployee("Sameer Shaik", "Programmer", "IT Development", "5000");
            while (choice != 6)
            {
                if (flag == 0)
                {
                    Console.WriteLine("Please enter 1 to view name of employee");
                    Console.WriteLine("Please enter 2 to view designation of employee");
                    Console.WriteLine("Please enter 3 to view department of employee");
                    Console.WriteLine("Please enter 4 to view salary of employee");
                    Console.WriteLine("Plese enter 5 to change the salary of the employee");
                    Console.WriteLine("Please enter 6 to exit");

                    choice = Convert.ToInt32(Console.ReadLine().ToString());
                    if (choice == 1)
                    {
                        MethodPointer PointerToNameDisplay = new MethodPointer(Sam.displayname);
                        displayinformation(PointerToNameDisplay);

                    }
                    if (choice == 2)
                    {
                        MethodPointer PointerToDesignationDisplay = new MethodPointer(Sam.displaydesignation);
                        displayinformation(PointerToDesignationDisplay);
                    }
                    if (choice == 3)
                    {
                        MethodPointer PointerToDepartmentDisplay = new MethodPointer(Sam.displaydepartment);
                        displayinformation(PointerToDepartmentDisplay);

                    }
                    if (choice == 4)
                    {
                        MethodPointer PointerToSalaryDisplay = new MethodPointer(Sam.displaysalary);
                        displayinformation(PointerToSalaryDisplay);
                    }
                    if (choice == 5)
                    {
                        flag = 1;
                        Sam.PassValDelegate = new Mydelegate(displaySalary);
                        Thread t1 = new Thread(new ThreadStart(Sam.setSalary));
                        t1.Start();


                    }
                }
            }
        }


        public static void displayinformation(MethodPointer M1)
        {
            Console.WriteLine("Displaying information you have chosen");
            M1();
            Console.WriteLine("Press any key to clear the screen: ");
            Console.ReadKey();
            Console.Clear();

           
        }

        public static void displaySalary(int salary)
        {
            Console.WriteLine("Renewed salary of the employee is : "+salary );
            Console.WriteLine("Press any key to clear the screen: ");
            Console.ReadKey();
            Console.Clear();
            flag = 0;
           
        }
      
       


       
       
    }
}



The above example must clear your understanding on passing and retrieving values from threads.

No comments:

Post a Comment