Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application window sent behind other windows on closing different thread (C#)

I'm writing a Windows Forms Application in C#.NET

On startup, the application displays a splash screen which is running in a separate thread. Whilst the splash screen is showing, the main application is initialising.

Once the main application has finished initialising, the main form of the application is displayed, and the splash screen still shows over the top.

Everything so far is as expected.

Then, the Splash screen is closed, which causes that thread to exit. For some reason, at the point, the main application windows gets sent behind all other open Windows, notably the Windows Explorer window where you clicked the .exe file to run the application in the first place!

What could be causing the windows to suddenly jump "behind" like this?

like image 793
user26906 Avatar asked Oct 10 '08 17:10

user26906


People also ask

How to Exit winforms application?

The proper method would be Application. Exit() .

Which of the following method is used to close the windows Form application*?

The Exit method stops all running message loops on all threads and closes all windows of the application.

Which method is used to Exit application?

exit() method calls the exit method in class Runtime. It exits the current program by terminating Java Virtual Machine.


1 Answers

Try calling .Activate() on your main window when your thread closes.

It's never been active, and thus has low Z-Order, so whatever is higher will naturally be above it. I had to fix this exact scenario in our app.

Don't forget! You may need to marshal the call to the correct thread using an Invoke()!

like image 98
Bob King Avatar answered Sep 27 '22 23:09

Bob King