It's simple enough to put a counter in the loop that exits the loop on too many iterations. In either case you have to work through the windows to get to the one you want.