written for http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23764469.html

This demonstrates how to be always on top and regain always on topness when you loose it. It features race condition detection.
You MUST understand that there can be only ONE window actually on top of every other windows. That is how the windows z-order is implemented. You cannot have 2 different numbers in a set of numbers both greater than all the numbers in the set. It's mathematically impossible.
That being said, when you try to get your on-top back and another application does the same thing, this will lead to a race condition meaning that both applications will try to get the always on top back from the other one and this will go into an infinite cycle. The race condition detection will prevent that.
Also note that at least yahoo messenger version 8 has a bug in which it will enter such a race condition and not get out of. So do write your application carefully from this point of view.

Source Code