OK it seems to boil down to Windows does not allow you to set something else to be the foreground window unless some conditions are met one of the possibilities being you are debugging

I guess to stop people doing nasty things, a bit like popups in broswers

Calling AllowSetForegroundWindow seems to work around this restriction

https://docs.microsoft.com/en-us/win...regroundwindow

This page also has a list of the conditions that have to be met

There is also CoAllowSetForegroundWindow which looks like a better call but i'm not quite sure how to get the information that needs to call it

https://docs.microsoft.com/en-us/win...regroundwindow

I have got AllowSetForegroundWindow to work by enum all the processes looking for the one I am interested in and passing that

Ironically calling AllowSetForegroundWindow stops it working in the debugger but then I guess Focus is messing with the focus