简体   繁体   中英

How can I ensure my process receive focus when one of its child process closes?

A little background

Our application (AppA) is calling a 3rd party application (AppB). We are simulating AppB's window as being modal to our application.

pseudo
while AppB_Running
  If AppA_focused
    SetForeGroundWindow(AppB_Hwnd)
  HandleMessage
SetForegroundWindow(AppA_HWnd)

This has been working for years without incidents, that is, until we started rolling out Windows 10. We started receiving reports that a random app (AppC) was sometime getting the focus when we closed AppB (Actually, given our user's tech expertise, the report were more along the line "Closing AppB kills AppA" but I digress). That behavior can be reproduced occasionnaly(<5%) on dev machine (But not yet while debugging...)

So far, test result suggest AppC is the application that had the focus before AppA.

Is there a way I can ensure AppA gets the focus back when AppB closes?

Extra notes

The problem can be reproduced even if AppA only ever loses focus to AppB. (AppC never being focused after AppA started)

While this does not directly answer my original question, it does give some more insight on the problem and might be useful to someone else.

Part of the issue was an oversight on our end. More specifically, the way we obtained AppB_HWnd. We basically picked the first top level window we encountered. In our current test case, that window happens to be an invisible windows ('MSCTFIME UI'). Calling SetForegroundWindow on an invisible window doesn't seem to cause problems per se, Windows seems to just bring to front one of the process' visible window. It is when said process does not have any visible windows left that things seems to go awry. That usually happen in our case between the moment AppB's main window is closed and the moment the process ends.

So... Don't call SetForegroundWindow on an invisible window

As for why we didn't have any trouble in Win7? Maybe the first window was always a visible one. Or maybe SetForegroundWindow behaved differently back then.

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