Did the above ever happen to you?
First things first: reproduction
Before doing further analysis, I needed a reliable way to reproduce the rearrangement of taskbar buttons.
For this reason I wrote a hang-test program, whose purpose is to hang 🙂
But just a hang wasn’t enough. A simple program that doesn’t respond for several seconds may lead to rearrangement on the taskbar, but it usually doesn’t.
After some experiments, I added a message box creation and some load on the CPU, which made it reproduce the issue most of the time on my PC.
You can grab the program here:
hang_test.zip (9.09 kB)
Investigating the issue
So, why does the rearrangement happen? Why isn’t it consistent, i.e. happens only at times?
The first thing you should know: when a window stops responding, Windows creates a special window of the class “Ghost”, which replaces the hung window.
You can read more about it here.
Now, how does explorer handle the replacement?
Explorer receives the “SysFrostedWindow” message (registered with RegisterWindowMessage) with two parameters: the replaced window and the replacing window.
Upon receiving the message, it just replaces the window of the taskbar button.
But the problem is that it doesn’t always receive the message on time!
So this is what happens:
- The window hangs.
- The system creates a “Ghost” window.
- Explorer receives the “SysFrostedWindow” message, and replaces the windows.
- The window becomes responsive again.
- The system removes the “Ghost” window.
- Before receiving the “SysFrostedWindow” message, explorer validates the taskbar button, which is no more valid as the “Ghost” window doesn’t exist anymore. The taskbar button gets removed.
- Explorer receives the “SysFrostedWindow” message, and creates a new taskbar button (as there’s nothing to replace).
When a hung window is replaced with a “Ghost” window, I write down the both windows. Then, when the taskbar button is accessed, I validate whether the “Ghost” window still exists, and if not, I restore the original window right away, without waiting for the “SysFrostedWindow” message.
The fix is available in 7+ Taskbar Tweaker v4.2.3, and is enabled by default.