c# subscribe an event when a function is complete?

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

namespace ConsoleApp13
{
    class Program
    {
        public class Subscriber
        {
            public static void Main()
            {
                Publisher publisher = new Publisher();


                publisher.BeginAdd += AddCallback;
                publisher.EndAdd += EndCallBack;

                Console.WriteLine(publisher.Multiply(2.3f, 4.5f));


                publisher.BeginAdd -= AddCallback;
                publisher.EndAdd -= EndCallBack;
                Console.WriteLine(publisher.Multiply(3.3f, 4.4f));

                Console.ReadLine();
            }
            public static void AddCallback(string message)
            {
                Console.WriteLine("Callback - " + message);
            }

            public static void EndCallBack(string message)
            {
                Console.WriteLine("Callback - " + message);
            }
        }
        public class Publisher
        {
            public delegate void Notify(string message); // Declare delegate.
            public event Notify BeginAdd;                // Declare event.
            public event Notify EndAdd;

            public float Multiply(float a, float b)
            {
                OnBeginAdd();                            // Raise event.
                OnEndAdd();
                return a * b;
            }
            private void OnBeginAdd()
            {
                if (BeginAdd != null)
                    BeginAdd("Starting multiplication!");      // Call callback method.
            }

            private void OnEndAdd()
            {
                if (EndAdd != null)
                    EndAdd("Completing multiplication!");
            }
        }
    }
}

How do I correct the syntax for adding OnEndAdd(); into the Multiply function so that it only does the call back after the completion of the function? I've tried adding it after the return statement but that obviously doesn't work, can't seem to figure out another way...

1 Answers

Once the Multiply function has returned the control moves away from the publisher, so some design changes would be necessary here.

Do you perhaps mean on completion of the multiply operation (and not the necessarily the entire function call), the below change would suffice.

        public float Multiply(float a, float b)
        {
            OnBeginAdd(); 
            var result = a * b;
            OnEndAdd();
        }

A prettier (tm) approach could be to create another class called e.g. OperationScope of type IDisposable which calls the OnBeginAdd / OnEndAdd for you - for example:

        public float Multiply(float a, float b)
        {
            using (new OperationScope(this)) //This is IDisposable and calls OnBeginAdd & OnEndAdd
            { 
                return a * b;
            }
        }

NOTE: There are potentially other similar ways instead of using an IDisposable class, such as passing a Func<xyz> that does the actual work (multiply) to another method which calls OnBeginAdd/OnEndAdd.

Related