By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

winform startup/splash screen

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" wrote:
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

Nov 17 '05 #2

P: n/a
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" <Ma*********@discussions.microsoft.com> wrote in message
news:81**********************************@microsof t.com...
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" wrote:
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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.