SplashScreen implementation

  • Thread starter Thread starter RYoung
  • Start date Start date
R

RYoung

Hello all,

I've got a need to display a splash screen while the main form loads. The
main form will be doing some initialization and posting messages to the
splash screen for display. Here is what I have in a nutshell (irrelevant
methods omitted):

public class SplashScreeen : Form
{
private delegate void DisplayStatusDelegate( string msg );

public void DisplayStatus( string msg )
{
if ( label1.InvokeRequired )
{
label1.BeginInvoke( new DisplayStatusDelegate( DisplayStatus ),
new object[]{ msg } );
}
else
{
label1.Text = msg;
}
}

public void ShowSplash()
{
this.ShowDialog();
}
}

// MainForm.TopMost = true
public class MainForm : Form
{
private void MainForm_Load( ... )
{
SplashScreen splash = new SplashScreen();
Thread splashThread = new Thread( new ThreadStart(
splash.ShowSplash ) );
splashThread.Start();

splash.DisplayStatus( "Initializing" );
Thread.Sleep( 1000 );
splash.DisplayStatus( "Sleeping" );
Thread.Sleep( 2000 );
splash.DisplayStatus( "Finished" );
Thread.Sleep( 1000 );

splash.Close();
splashThread.Abort();

Activate();
}
}

Everything works as expected. The Thread.Sleep calls will be replaced with
intialization routines for the app.
Any comments as to what could be wrong with this implementation is
appreciated.

Ron
 
Interesting...Thanks for the reply and link :) Will have a look.

Ron

Colin Neller said:
A couple of things: Thread "A" creates the form and closes it, but
thread "B" calls ShowDialog. All of these things should be on the same
thread. Your implementation of DisplayStatus looks good, though.

You might take a look at this splash screen example:
http://www.codeproject.com/csharp/PrettyGoodSplashScreen.asp. Look at
the comments at the bottom of the page if you have trouble with focus.

Hope that helps,
Colin Neller
http://colinneller.com/

Hello all,

I've got a need to display a splash screen while the main form loads. The
main form will be doing some initialization and posting messages to the
splash screen for display. Here is what I have in a nutshell (irrelevant
methods omitted):

public class SplashScreeen : Form
{
private delegate void DisplayStatusDelegate( string msg );

public void DisplayStatus( string msg )
{
if ( label1.InvokeRequired )
{
label1.BeginInvoke( new DisplayStatusDelegate(
DisplayStatus ),
new object[]{ msg } );
}
else
{
label1.Text = msg;
}
}

public void ShowSplash()
{
this.ShowDialog();
}
}

// MainForm.TopMost = true
public class MainForm : Form
{
private void MainForm_Load( ... )
{
SplashScreen splash = new SplashScreen();
Thread splashThread = new Thread( new ThreadStart(
splash.ShowSplash ) );
splashThread.Start();

splash.DisplayStatus( "Initializing" );
Thread.Sleep( 1000 );
splash.DisplayStatus( "Sleeping" );
Thread.Sleep( 2000 );
splash.DisplayStatus( "Finished" );
Thread.Sleep( 1000 );

splash.Close();
splashThread.Abort();

Activate();
}
}

Everything works as expected. The Thread.Sleep calls will be replaced
with
intialization routines for the app.
Any comments as to what could be wrong with this implementation is
appreciated.

Ron
 
Back
Top