0

I'm dealing with a bug relating to a C# WinForms app that starts up with a splash screen, then closes the splash screen and opens a login form. On some computers everything works fine. On others, the login form appears, but with a flashing title bar, which after a few flashes, loses focus altogether. The cursor is still flashing in the "User name" text box, but the app does not have focus, and when you start typing, nothing happens, which is very annoying for the user.

There seems to be no difference what OS is running (we've tried Windows 7 and Server 2008), and we have been particular not to have any keyboard or mouse input after starting up the app.

Now - does anyone have any idea what could be causing the app to lose focus?
Alternatively, how would you debug this issue? We have been unable to replicate the problem in the Visual Studio debug environment, but that doesn't entirely surprise me because I'd guess it's an issue of how the compiled app interacts with the OS... or am I wrong?

EDIT #1: I thought this was solved by @vinodpthmn, by ensuring that the splash screen is properly closed before the login form appears, but this appears not to have helped. So I created a logger to track all the events and threads, and found the following interesting log entries:

Thread 01 - 2012/03/29 12:51:09.693 - Show splash screen
Thread 01 - 2012/03/29 12:51:20.350 - Splash screen closed
Thread 01 - 2012/03/29 12:51:20.490 - Login Form Activated
Thread 01 - 2012/03/29 12:51:20.522 - Login Form Load
Thread 01 - 2012/03/29 12:51:25.694 - Login Form deactivated
Thread 01 - 2012/03/29 12:51:25.694 - Active form =
Thread 01 - 2012/03/29 12:51:25.694 - Active app =

Those last lines showing the active form and app respectively display Form.ActiveForm and the currently active app in Windows (code for this here). And they are both empty/null. This is so, even if in the Login form Load I call Activate() or SetForegroundWindow() as suggested by @memetolsen. The login form never even receives focus!

Any ideas?

EDIT #2: Well, I just eliminated the splash screen, and now the login form gets focus. Replacing the splash screen reintroduces the bug. But I have put code in to ensure that the splash screen is disposed - not just closed - before I try to open the login form.

Could it make a difference that the splash screen is opened with Application.Run(frmSplash)?

Community
  • 1
  • 1
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387

4 Answers4

5

Could it make a difference that the splash screen is opened with Application.Run(frmSplash)

Certainly, doing it this way guarantees you'll have this kind of problem. This trouble is caused because for a split second you have no window that can receive the focus. The splash screen is gone, your main window still needs a handful of milliseconds to get created and visible. This forces Windows to find another window to give the focus to. Since it can't be your window, it will pick the window of another application. Your main window will not have the focus when appears.

The exact rules that the window manager uses are not clear to me, this focus loss doesn't always happen. I'm guessing it is time related.

Solve your problem by using the excellent and trouble free support for splash screens that's already built into the framework. You'll find the code to use it in this answer. If you don't want to use it then rework your code to ensure the main window is displayed before you close the splash screen.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

I have faced a similar issue earlier, the problem was due to improper closing of splash screen. I suspect the focus might be on that form (not sure though).

  1. make sure you unload/close splash screen form properly
  2. login screen is shown after proper unload of splash screen
  3. login form is shown as modal to instruct the execution pointer to wait until next user interaction
vinodpthmn
  • 1,062
  • 14
  • 28
1

I suggest you set up remote debugging. This will enable you to debug the application remotely from your development machine.

Polyfun
  • 9,479
  • 4
  • 31
  • 39
1

Maybe it would be a solution if you put this:

this.Activate();

or this:

SetForegroundWindow(this.Handle.ToInt32());

in the load event of the form.

If this doesn't work, try using a timer which ferforms this code after a certain time.

Memet Olsen
  • 4,578
  • 5
  • 40
  • 50