0

For specific reasons I have to run a Windows forms app on a Windows server (2012 RC2, hosted by LiquidWeb). The forms app itself is spawned from a console application upon specific file system changes.

The purpose of the app is to automate a task, using a WebBrowser control. To complete the task we need to, at various intervals, send key input to the forms app (to upload and download files). In order for this to work, the app need to have focus. I'm using the code in this thread to give focus to the app, without problems.

This all works well, as long as I'm logged in (via Remote Desktop). However, when I close the remote desktop session (without logging out the user), there seems to be no way for the forms app to receive focus.

The spawning works well, the forms app initiates and starts its browser session as planned, but it just wont receive focus. When I log back in using Remote Desktop, I can see the forms app behind the console app.

I've tried any combination of SetForegroundWindow/ShowWindow I can think of, both directly from the forms app and from the console app that spawns it. I've also tried running an AutoHotKey script (containing a WinActivate command) from the console app after spawning the forms app , with the same result (it works as long as I have a "visual" session).

Any ideas whatsoever?

Community
  • 1
  • 1
Magnus Andersson
  • 234
  • 1
  • 2
  • 10

1 Answers1

0

So, this seems to be all related to the fact that the remote computer gets locked when one leaves the Remote Desktop session, thus preventing the app in question to control the focus.

The solution is to log out using the tscon command, as described in this article. Basically, log off by running a bat file with the following content:

for /f "skip=1 tokens=3" %%s in ('query user %USERNAME%') do (
  %windir%\System32\tscon.exe %%s /dest:console
)
Magnus Andersson
  • 234
  • 1
  • 2
  • 10