public void Hello_Event() { var hello = new HelloHandler(); var vp = new ValueProcessor(hello); // Listen to ValueChanged Event with normal and anonymous function hello.ValueChanged += (s, e) => { Trace.WriteLine($"From anonymous: New value is {e}"); }; hello.ValueChanged += Hello_ValueChanged; // Difference with Event and multicastdelegate is that although it's public, only owner can set it to null // hello.ValueChanged = null; // Running for 5 seconds var sw = Stopwatch.StartNew(); while (sw.ElapsedMilliseconds < 5000) { } vp.Stop(); // Problem with anonymous functions is that you can't remove those hello.ValueChanged -= Hello_ValueChanged; hello.CurrentValue = 8; // From anonymous function still prints }
public ValueProcessor(HelloHandler helloHandler) { _helloHandler = helloHandler; Task.Factory.StartNew(UpdateValue, _cts.Token); }