0

I have a splash screen that loads before my main form load. What happens is the splash screen closed before the main screen loads. I added the splash screen, as my application takes a while to load based on the license key a thread which I posted here.

I did search but not sure of how to implement in my code. I came across Backgroundworker too, but again not sure of how to proceed.

Main code:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Thread th = new Thread(new ThreadStart(ShowSplashScreen));
    th.Start();
    Thread.Sleep(5000);
    Application.Run(new frmMain());
    th.Abort();
}

static void ShowSplashScreen()
{
    Application.Run(new frmSplashScreen());
}

Splash Screen:

In the form load

timer1.Start();
timer1.Interval = 600;
progressBar1.Maximum = 10;

private void timer1_Tick(object sender, EventArgs e)
{
    if (progressBar1.Value != 10)
    {
        progressBar1.Value++;
    }
    else
    {
        timer1.Stop();
        timer1.Enabled = false;
        this.Close();
    }
}
Community
  • 1
  • 1
Adrian
  • 333
  • 6
  • 20
  • Just a suggestion, and forgive me if it's silly, but it's saturday and I'm at home and just awoken but... couldn't you call Application.Run(new frmMain()); from the Splash screen code instead of the Main() code. Just before the this.Close() line? – CMPerez Mar 16 '13 at 07:22
  • Have a look at this answer to overcome your [Splashscreen closing before the main process completes](http://stackoverflow.com/questions/13206926/vb-net-progressbar-backgroundworker/13486676#13486676) – Jeremy Thompson Mar 16 '13 at 07:52

1 Answers1

1

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.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Gary thanks for the code. I did come across this before, as you rightly said, Microsoft.Visual basic turned me off as I am using C#. – Adrian Mar 16 '13 at 07:56
  • @gary I tried the code am getting an error like this A startup form has not been specified – Adrian Mar 16 '13 at 09:39