winform startup/splash screen

T

Tom

Hi all

In winform application I am trying to start 2 forms I am trying to show the
2nd winform on a separate thread.
using
Thread mainapp = new Thread(new ThreadStart(loadmainscm));

mainapp.Start();

//where

private void loadmainscm() {

SCM.frmSplash frmsplash = new frmSplash();

frmsplash.Show();

Thread.Sleep(10000);

}


However the 2nd form will show but the graphics and controls are not
rendered when I use Thread.Sleep(10000); ... if I do not use
Thread.Sleep(10000); then the form will show and then quickly close itself,
I believe the thread will stop itself once the form has started.

what I am trying to achieve is a splash screen or a startup screen. does
anyone have any better idea ? or how I can achieve this ?

Thanks
Tom
 
G

Guest

Hi Tom,
I would do something like the following:

1. Create a splash form (like you have), look at LaunchSplash and
CloseSplash. These two methods are static to easily help launch and close
the form:

//SplashScreen.cs

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication4
{
public class SplashScreen : Form
{
private static Thread _splashLauncher;
private static SplashScreen _splashScreen;

private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "SplashScreen";
}
#endregion

private SplashScreen()
{
InitializeComponent();
}

public static void ShowSplash()
{
//Show the form in a new thread
_splashLauncher = new Thread(new ThreadStart(LaunchSplash));
_splashLauncher.IsBackground = true;
_splashLauncher.Start();

}

private static void LaunchSplash()
{
_splashScreen = new SplashScreen();

//Create new message pump
Application.Run(_splashScreen);
}

private static void CloseSplashDown()
{
Application.ExitThread();
}

public static void CloseSplash()
{
//Need to get the thread that launched the form, so
//we need to use invoke.
MethodInvoker mi = new MethodInvoker(CloseSplashDown);
_splashScreen.Invoke(mi);
}
}
}



2. Need to make a call to launch the splash screen, this can be done in your
main method:

[STAThread]
static void Main()
{
//Show the flash ASAP
SplashScreen.ShowSplash();

Application.Run(new Form1());
}


3. Finally in the constructor for your main form do all the loading work you
have to do (I put a thread.Sleep to simulate this) and then close the
SplashScreen last:

//Form1.cs

public Form1()
{
//Simulate some work
System.Threading.Thread.Sleep(5000);

InitializeComponent();

//Close the splash
SplashScreen.CloseSplash();
}


Hope that helps
Mark R Dawson
http://www.markdawson.org
 
T

Tom

Thanks Mark

I really liked your explaination. It worked and helped me alot !

Thankyou for taking the time to draw up the sample code it was very helpful

Tom

Mark R. Dawson said:
Hi Tom,
I would do something like the following:

1. Create a splash form (like you have), look at LaunchSplash and
CloseSplash. These two methods are static to easily help launch and close
the form:

//SplashScreen.cs

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication4
{
public class SplashScreen : Form
{
private static Thread _splashLauncher;
private static SplashScreen _splashScreen;

private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "SplashScreen";
}
#endregion

private SplashScreen()
{
InitializeComponent();
}

public static void ShowSplash()
{
//Show the form in a new thread
_splashLauncher = new Thread(new ThreadStart(LaunchSplash));
_splashLauncher.IsBackground = true;
_splashLauncher.Start();

}

private static void LaunchSplash()
{
_splashScreen = new SplashScreen();

//Create new message pump
Application.Run(_splashScreen);
}

private static void CloseSplashDown()
{
Application.ExitThread();
}

public static void CloseSplash()
{
//Need to get the thread that launched the form, so
//we need to use invoke.
MethodInvoker mi = new MethodInvoker(CloseSplashDown);
_splashScreen.Invoke(mi);
}
}
}



2. Need to make a call to launch the splash screen, this can be done in
your
main method:

[STAThread]
static void Main()
{
//Show the flash ASAP
SplashScreen.ShowSplash();

Application.Run(new Form1());
}


3. Finally in the constructor for your main form do all the loading work
you
have to do (I put a thread.Sleep to simulate this) and then close the
SplashScreen last:

//Form1.cs

public Form1()
{
//Simulate some work
System.Threading.Thread.Sleep(5000);

InitializeComponent();

//Close the splash
SplashScreen.CloseSplash();
}


Hope that helps
Mark R Dawson
http://www.markdawson.org



Tom said:
Hi all

In winform application I am trying to start 2 forms I am trying to show
the
2nd winform on a separate thread.
using
Thread mainapp = new Thread(new ThreadStart(loadmainscm));

mainapp.Start();

//where

private void loadmainscm() {

SCM.frmSplash frmsplash = new frmSplash();

frmsplash.Show();

Thread.Sleep(10000);

}


However the 2nd form will show but the graphics and controls are not
rendered when I use Thread.Sleep(10000); ... if I do not use
Thread.Sleep(10000); then the form will show and then quickly close
itself,
I believe the thread will stop itself once the form has started.

what I am trying to achieve is a splash screen or a startup screen. does
anyone have any better idea ? or how I can achieve this ?

Thanks
Tom
 

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