You go through all of the effort to kick off a separate thread for your splash screen, which means you have the right idea. The problem is the implementation is a bit off. The call to Thread.Sleep
actually sleeps the current thread, so your main UI thread is completely dead for about 5 seconds, doing absolutely nothing when what it should be doing is loading your main form in the background while the splash screen is displayed. After Thread.Sleep
returns, you call Application.Run
to launch the main form, but by that time it's too late—the splash screen has already stayed up for as long as the code tells it to and it closes itself.
You could probably fix the problem by moving the call to Thread.Sleep
to the splash screen thread, but that's a pretty ugly and hackish approach to be honest. Aside from the inherent problems with sleeping a thread, the real point of a splash screen (as you yourself concede) is to give the user something visually interesting and reassuring to look at while you complete some lengthy task. Most users don't particularly like splash screens (okay, actually, it seems like most people hate them), but they do arguably have a purpose, albeit a limited one. When you use something like Thread.Sleep
, effectively forcing your splash screen to camp out on the screen doing nothing, you're just spinning CPU cycles and wasting the user's time for no reason. They're going to be a lot less forgiving of that. I'm sure that's probably not your plan, but it's often what ends up happening.
Wouldn't it be nice if you could actually show the splash screen while you're doing the lengthy validation at the same time in the background? Indeed, that's the "correct" implementation for a splash screen, but it's awfully hard to make it work just right. Synchronization and cross-threading in UI code is tricky to write and even trickier to debug.
Instead, I recommend that you punt. Rip out your code to manually display the splash screen and all of its Thread.Sleep
ugliness, and instead replace it with the built-in splash screen framework. Most people don't know about it, because the crafty Microsoft programmers hid it under the Microsoft.VisualBasic
namespace where C# folks can't find it. Don't let that fool you, though. It works perfectly well with C# applications and makes your life a whole lot easier. Why should those VB.NET guys get to hoard all the good stuff for themselves?
This framework will handle everything for you automatically, all you need to do is provide it with a splash screen form class (which you appear to already have, named frmSplashScreen
). You can find complete instructions for using it in my answer here, including sample code.