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);
}
}