Caveats

While running the example application provided with this chapter, you might notice that the number stops incrementing when another application is brought to the foreground. If you make the example application the active window, the number continues to increment. This is because the main thread of our application is considered the foreground thread when the application is active. A foreground thread is the main thread of an application that is currently activated. All other threads are considered background threads. This design allows the system to give more CPU time to this thread, thus boosting the performance of the active application. When our application is deactivated, another application’s main thread will become the foreground thread, and the main thread of our application will become a background thread. After our application’s main thread ceases to be a foreground thread, the system will not call the hook installed on that thread.

The system calls the WH_FOREGROUNDIDLE hook only immediately before the application goes into an idle state, not while the application is in an idle state. Therefore, this hook can prevent the application from going into an idle state if the code within the hook causes a message to be sent to the window’s message queue. The WaitMessage function will see this new message and return, preventing the application from entering an idle state. You can see evidence of this in our sample application: the number in the text box keeps incrementing ...

Get Subclassing and Hooking with Visual Basic now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.