SplashScreen implementation

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
 
R

RYoung

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top