Callback Interfaces in C#

Not only for polymorphism, interfaces can also be used for a callback mechanism. With callback mechanism, objects can engage in two-way communication. In this article, we will discuss how to achieve callback mechanism with interfaces in C#.

Open Microsoft Visual Studio 2015 => Create new Console Application and name it as CSharpCallbackInterfaces. In this example, we will measure processing of any request and returns whenever it completes 100%. Create callback interface as shown below.

public interface IProcessing

{

        void AboutToFinish(string sMsg);

        void Finished(string sMsg);

}

As shown above we have two methods AboutToFinish() & Finished(). AboutToFinish() method needs to call whenever the processing percentage is greater than 50% and less than 100%. Finished() method needs to call once the processing percentage becomes 100%. Callback interfaces are not implemented by the main object directly instead implemented by helper object called sink object. Let’s create sink class which implements IProcessing interface as shown below.

public class ClsSink : IProcessing

{

        public void AboutToFinish(string sMsg)

        {

            Console.WriteLine(sMsg);

        } 

        public void Finished(string sMsg)

        {

            Console.WriteLine(sMsg);

        }

 } 

 

Now we have to create the class which takes the sink reference as input. This class should have methods for attaching and detach the sink reference as shown below.

public class ClsProcessing

{

        ArrayList clientSinks = new ArrayList(); 

        public void Attach(IProcessing sink)

        {

            clientSinks.Add(sink);

        } 

        public void Detach(IProcessing sink)

        {

            clientSinks.Remove(sink);

        }

 }

As of now we have created the callback mechanism by using interfaces. Let’s add the notification method to the class ClsProcessing as shown below.

public class ClsProcessing

{

        ArrayList clientSinks = new ArrayList(); 

        public void Attach(IProcessing sink)

        {

            clientSinks.Add(sink);

        } 

        public void Detach(IProcessing sink)

        {

            clientSinks.Remove(sink);

        } 

        int iPercentage = 0;

        public void Process(int iDelta)

        {

            if (iPercentage >= 100)

            {

                foreach (IProcessing sink in clientSinks)

                    sink.Finished("100% completed");

            }

            else

            {

                iPercentage = iPercentage + 10;

 

                foreach (IProcessing sink in clientSinks)

                    sink.AboutToFinish(iPercentage.ToString() + "% completed");

            }

        }

 }

Let’s implement the Main() method as shown below which shows the Callback interfaces functionality.

class Program

{

        static void Main(string[] args)

        {

            ClsProcessing obj = new ClsProcessing(); 

            //create sink object

            ClsSink sink = new ClsSink();

 

           //attach sink object

            obj.Attach(sink); 

            for (int i = 0; i < 10; i++)

                obj.Process(20); 

            //detach sink object

            obj.Detach(sink); 

            Console.ReadLine();

        }

 }

The output is as shown below.

                                                                                                                                                CSharpCallbackInterfaces.zip