简体   繁体   中英

Determining the source of a window activation event in WPF

I've got a complex WPF application with windows on multiple threads. Quite reproducibly, it happens that when I open and close a new window, the focus (activation) does not go back to the last activated window. Instead an (usually) older window gets activated. Since the workflow is "working in window A", "opening windows B, C from A", "working with B, C", "open window D from B", "close D", I'd need windows to return focus to B, but actually it goes to A.

I've already taken a look at stacktraces around the Window.Activated and Window.Deactivated events, but those come directly from the WPF infrastructure and do not show any clues. I've already tried Spy++, but its usefulness is... limited.

How can I debug this further?

Update: The phenomenon vanished after I stopped blocking the thread of B for longer periods of time when creating D. It seems to be the case that the desktop window manager becomes confused, when threads block their message pump. I've plastered over the problem by pushing the message-pump blockers onto the Dispatcher with background priority before creating D. This seems to clear up any DWM confusion and windows activation doesn't act up anymore. I'd still be interested in pointers to a more in-depth solution/analysis.

You might not want to hear this but in these situations the next step I take is to examine the reference source for the the methods immediately in the stack of the relevant phenomenon. I don't know if you've use reference source before:

Get this set up and then set up a breakpoint at the usual spot, say a focus event. Now look up the stack. You should be able to double-click and see source code for most of the stack frames. Of course it's a crap shoot but in any case better than looking at method names.

If you find you want to inspect some variables on the reference source side you'll find they don't work. Use this fix:

I know this is not very specific but this is what I would try next.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM