concept multiple event in category c#

appears as: multiple events
Real-World Functional Programming with Examples in F# and C#

This is an excerpt from Manning's book Real-World Functional Programming with Examples in F# and C#.

Understanding reactive applications is much more difficult. A typical C# application or GUI control that needs to react to multiple events usually involves mutable state. When an event occurs, it updates the state and may run more code in response to the event, depending on the current state. This architecture makes it quite difficult to understand the potential states of the application and the transitions between them. Using asynchronous workflows, we can write the code in a way that makes the control flow of the application visible even for reactive applications.

In all the previous examples, we used the AwaitObservable method, which takes just one event as an argument. However, the source code for the book also implements an overload, which allows us to specify multiple events and waits for the first occurrence of any of the specified events, ignoring any subsequent occurrences. In our case, this means that the call ❶ will block until either a mouse is moved or a key is pressed; it will run the processing code. If the processing ends with a recursive call, then AwaitObservable will be called again to wait for the next event, but otherwise the next occurrence will be ignored. To better understand how the overload taking two parameters works, let’s look at its type signature: