// This function allows worker classes to write something that can be signaled to observers. public void SendMessage(string message, string reason) { // Here we can perform some tests before sending the message if (string.IsNullOrEmpty(reason)) { throw new ArgumentNullException(nameof(reason)); } if (MessageReceived == null) { return; } var printMessageArgs = new MessageArgs(message, reason); OnMessageReceived(printMessageArgs); }
protected virtual void OnMessageReceived(MessageArgs e) { // This raises our event observers listen to. MessageReceived?.Invoke(this, e); }
private static void PrinterOnMessageReceived(object sender, MessageArgs messageArgs) { Console.WriteLine($"Sender: {sender}{Environment.NewLine}" + $"Message: {messageArgs.Message}{Environment.NewLine}" + $"Reason: {messageArgs.Reason}{Environment.NewLine} "); }