1

I'm having trouble invoking delegates that were added after I pass the event to a specific class, I thought that the delegate get updates as objects..

For example this the class that I'm passing the delegate to:

Updated code (due to some question in the comments)

This is actually +- how I need to run it, Where "ExecutorOnCalculationCompleted" is never invoking (please ignore sleep, and synchronization, I shrink my code to the needed parts)

    class Executor
{

    public delegate void CalculationCompletEventHandler(int score);

    public event CalculationCompletEventHandler CalculationCompleted;

    public void Start()
    {
        CalculationCompleted += OnCalculationCompleted;
        Plus plus = new Plus(1, 2, CalculationCompleted);
        Minus minus =  new Minus(5, 2, CalculationCompleted);
        Multi multi =  new Multi(5, 2, CalculationCompleted);
        ...
        ...    They will work async...
    }

    private void OnCalculationCompleted(int score)
    {
        Console.WriteLine("OnCalculationCompleted , score=" + score);
    }
}

class Plus
{
    private Executor.CalculationCompletEventHandler _calculationCompletEventHandler;
    private int a;
    private int b;

    public Plus(int a, int b,Executor.CalculationCompletEventHandler calculationCompletEventHandler)
    {
        this.a = a;
        this.b = b;
        _calculationCompletEventHandler = calculationCompletEventHandler;
    }

    public void Calculate()
    {
        _calculationCompletEventHandler?.Invoke(a+b);
    }
}



class Program
{

    static void Main(string[] args)
    {
        Executor executor = new Executor();
        executor.Start(); // async action
        executor.CalculationCompleted += ExecutorOnCalculationCompleted;
        ...
    }

    // This method doesn't get invoked when the class inside Executor fire the event.
    private static void ExecutorOnCalculationCompleted(int score)
    {
        Console.WriteLine("ExecutorOnCalculationCompleted , score=" + score);
    }
}
Aviram Fireberger
  • 3,910
  • 5
  • 50
  • 69

1 Answers1

2

You are passing the delegate directly in the constructor. I think you want to put the event into the class. Example:

class Plus
{
    public delegate void CalculationCompletEventHandler(int score);
    public event CalculationCompletEventHandler CalculationCompleted;

    private int a;
    private int b;

    public Plus(int a, int b)
    {
        this.a = a;
        this.b = b;
    }

    public void Calculate()
    {
        if (CalculationCompleted != null)
        {
            CalculationCompleted(a + b);
        }
    }
}

you can now use it like that:

void Main()
{
    Plus plus = new Plus(1, 2);
    plus.CalculationCompleted += OnCalculationCompleted;
    plus.Calculate();

    plus.CalculationCompleted += OnCalculationCompleted2;
    plus.Calculate();        
}

private void OnCalculationCompleted(int score)
{
    Console.WriteLine("OnCalculationCompleted , score=" + score);
}

private void OnCalculationCompleted2(int score)
{
    Console.WriteLine("OnCalculationCompleted2 , score=" + score);
}

As mentioned in the comment, you maybe only need the event itself if the delegate type is used in different parts of your program.

After Update in Question

If you want the event in the executor, I would simply pass an action to each of your classes like Plus, Multiply, ... and invoke the event from the executor:

class Executor
{
    public delegate void CalculationCompletEventHandler(int score);
    public event CalculationCompletEventHandler CalculationCompleted;

    public void Start()
    {
        CalculationCompleted += OnCalculationCompleted;
        Plus plus = new Plus(1, 2, FireEvent);        
    }

    private void FireEvent(int score)
    {
        if (CalculationCompleted != null)
        {
            CalculationCompleted(score);
        }
    }

    private void OnCalculationCompleted(int score)
    {
        Console.WriteLine("OnCalculationCompleted , score=" + score);
    }
}

class Plus
{
    private int a;
    private int b;
    private Action<int> completionAction

    public Plus(int a, int b, Action<int> completionAction)
    {
        this.a = a;
        this.b = b;
        this.completionAction = completionAction;       
    }

    public void Calculate()
    {
        this.completionAction(a + b);
    }
}
Nico
  • 3,542
  • 24
  • 29