Exercise: Patient Vitals Monitor

Build a complete publisher-subscriber system using EventHandler<T> and custom event arguments.

Problem statement

You are writing software for an intensive care unit (ICU) patient monitor. The monitor constantly checks the patient's heart rate and oxygen saturation (SpO2). If the heart rate drops below 50 or exceeds 120, or if the SpO2 drops below 90, the system must broadcast an emergency alert containing the exact vitals. You must build a complete utility that simulates reading a stream of patient data and triggers the alerts appropriately.

Task requirements

  • Create a VitalsArgs class that inherits from EventArgs and holds HeartRate and SpO2 integer properties.

  • In VitalsMonitor, create a public event using the built-in EventHandler<T> delegate.

  • Implement the RecordVitals method to check the safety thresholds and raise the event if the patient is in danger.

  • Instantiate the monitor and subscribe an alert system that prints "CRITICAL: HR is {HeartRate}, SpO2 is {SpO2}".

Constraints

  • You must strictly follow the standard .NET event pattern (EventHandler<TEventArgs>).

  • RecordVitals should raise the event passing this as the sender and a new instance of VitalsArgs.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Custom event argument classes simply hold data. They need read-only properties and a constructor to set those properties.

  • The standard signature for a subscriber of EventHandler<T> requires two parameters, typically named sender and e.

  • Inside your lambda expression, you will access the data using e.HeartRate and e.SpO2.

Exercise: Patient Vitals Monitor

Build a complete publisher-subscriber system using EventHandler<T> and custom event arguments.

Problem statement

You are writing software for an intensive care unit (ICU) patient monitor. The monitor constantly checks the patient's heart rate and oxygen saturation (SpO2). If the heart rate drops below 50 or exceeds 120, or if the SpO2 drops below 90, the system must broadcast an emergency alert containing the exact vitals. You must build a complete utility that simulates reading a stream of patient data and triggers the alerts appropriately.

Task requirements

  • Create a VitalsArgs class that inherits from EventArgs and holds HeartRate and SpO2 integer properties.

  • In VitalsMonitor, create a public event using the built-in EventHandler<T> delegate.

  • Implement the RecordVitals method to check the safety thresholds and raise the event if the patient is in danger.

  • Instantiate the monitor and subscribe an alert system that prints "CRITICAL: HR is {HeartRate}, SpO2 is {SpO2}".

Constraints

  • You must strictly follow the standard .NET event pattern (EventHandler<TEventArgs>).

  • RecordVitals should raise the event passing this as the sender and a new instance of VitalsArgs.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Custom event argument classes simply hold data. They need read-only properties and a constructor to set those properties.

  • The standard signature for a subscriber of EventHandler<T> requires two parameters, typically named sender and e.

  • Inside your lambda expression, you will access the data using e.HeartRate and e.SpO2.

C# 14.0
var monitor = new VitalsMonitor();
// 1. Subscribe to the event using a lambda expression.
// Note: The lambda must accept (sender, e)
Console.WriteLine("Starting patient monitoring...");
monitor.RecordVitals(75, 98);
monitor.RecordVitals(80, 97);
monitor.RecordVitals(130, 85);
monitor.RecordVitals(45, 95);
// --- Class Definitions ---
// 2. Make this class inherit from EventArgs
public class VitalsArgs
{
// 3. Add HeartRate and SpO2 properties, and a constructor
}
public class VitalsMonitor
{
// 4. Declare the event using EventHandler<VitalsArgs>
public void RecordVitals(int heartRate, int oxygenLevel)
{
Console.WriteLine($"Reading - HR: {heartRate}, SpO2: {oxygenLevel}");
// 5. Check if heartRate < 50 OR heartRate > 120 OR oxygenLevel < 90
// 6. If true, invoke the event safely
}
}