How to monitor a switch's state and do X if switch has remained in state B

PeterH:
Remember the previous state of the switch, and only record the 'start time' when you see the switch change to state B. This is what Arrch refers to as the edge time.

Yup. The "Edge time" refers to the time in which the state changes. The rising or positive edge is when the state goes from low to high, and the falling or negative edge is low to high. Do you will need to keep track of one of those by keeping track of the last state.

So the pseudo code for tracking the positive edge would be:

get current state

if last state is low and current state is high
// Positive Edge
set edge time to current

set last state to current state

if edge time minus current time is greater than threshold time
// Do something here