Wednesday 11 January 2012

Asynchronous Processing

Introduction
                        Hi, today we will demonstrate asynchronous processing using delegates and events. One might think what the need of doing work   asynchronously in windows environment. Yes, it is required. By sample example we will prove this.
The Program
                        In this program we will develop a class will have certain events and delegates to handle those events. Our class will have a function to perform task and continuously changes the status through raising events.
public class Task
    {
        public delegate void UpdateStatusEventHandler(string text, int total, int current);
        public delegate void UpdateTextEventHandler(string text);
        public delegate void UpdateProgressBarEventHandler(int total, int value);

        public event UpdateStatusEventHandler UpdateStatus;
        public event UpdateTextEventHandler UpdateText;
        public event UpdateProgressBarEventHandler UpdateProgressBar;

        ……….
    }
                As shown in code we define 3 events for updating status, text and progress bar value as per task perform. For handling these events we define 3 delegates. As we all knows delegate works asynchronously, so whenever we raise these events, they will execute asynchronously.
This event handled from Windows Form. Code to handle these events are as follows.
            Task task = new Task();
            task.UpdateProgressBar += new Task.UpdateProgressBarEventHandler(task_UpdateProgressBar);
            task.UpdateStatus += new Task.UpdateStatusEventHandler(task_UpdateStatus);
            task.UpdateText += new Task.UpdateTextEventHandler(task_UpdateText);
in order to handle these events we’ve to register them first. To register them first create instance of class which owns those events. As you can see delegates which we defined in that class are working as event handler and each event handler has reference of a function having code for handling this events.
        private void task_UpdateStatus(string text, int total, int current)
        {
            eUpdateStatus(lblStatus, text, total, current);
        }

        private void task_UpdateText(string text)
        {
            eUpdateText(lblText, text);
        }

        private void task_UpdateProgressBar(int total, int value)
        {
            eUpdateProgressBar(pbTask, total, value);
        }
Now let us see how to update windows contron state asynchronously. For that you have to define delegates. As we knows delegates are special typed class which can point function having same signature. We’ll define delegates and functions that work asynchronouly for us.
        private delegate void delUpdateStatus(Label lbl, string text, int total, int current);       
        private void mUpdateStatus(Label lbl, string text, int total, int current)
        {
            if (lbl.InvokeRequired)
            {
                lbl.BeginInvoke(new delUpdateStatus(mUpdateStatus1), lbl, text, total, current);
            }
        }

        private void mUpdateStatus1(Label lbl, string text, int total, int current)
        {
            lbl.Text = string.Format("{0}/{1}\n {2}", current, total, text);
            lbl.Update();
            lbl.Parent.Update();
            this.Update();
        }
As we shown above we defined delegate and its function for updating label status. Here you notice BeginInvoke method which execute asynchronously for updating control status


No comments:

Post a Comment