PDA

Click to See Complete Forum and Search --> : Splash form on separate thread


BrianHawley
Nov 30th, 2005, 04:05 AM
This is my first real attempt at threading and I could use some comment on whether or not I'm following good practice.

The class is passed a form, which it runs on a separate thread as a splash form. The form fades in and then fades out when unloaded.

This is the main code:


using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Vanilla
{
/// <summary>
/// Class to contain the splash form
/// </summary>
class Splash
{
private Form splashForm; //The splash form
public Boolean FadeAway; //Set true to initiate fade and exit

/// <summary>
/// Class to run a splash form
/// </summary>
/// <param name="SplashForm">The form to be used as a splash form</param>
public Splash(System.Windows.Forms.Form SplashForm)
{
splashForm = SplashForm;
}

/// <summary>
/// Start displaying the splash form
/// </summary>
public void Start()
{

//Set up the form
splashForm.Opacity = 0;
splashForm.TopMost = true;
splashForm.Show();

//Fade it in
while (splashForm.Opacity < 1)
{
splashForm.Opacity += 0.1;
Thread.Sleep(100);
}

//Wait until exit
while (!FadeAway)
{
Thread.Sleep(100);
}

//Fade it out
while (splashForm.Opacity > 0)
{
splashForm.Opacity -= 0.1;
Thread.Sleep(100);
}

//Terminate
splashForm.Dispose();

}
}

/// <summary>
/// Class to run Splash class in a separate thread
/// </summary>
class SplashThreader
{
private Thread splashThread; //Thread to run the Splash class
private Splash splash; //Instance of the Splash class

/// <summary>
/// Runs a splash form in a separate thread
/// </summary>
/// <param name="splashForm">The form to be used as a splash form</param>
public SplashThreader(Form splashForm)
{
splash = new Splash(splashForm); //New instance of the Splash class
splashThread = new Thread(new ThreadStart(splash.Start)); //Thread to run it in
splashThread.Start(); //Start the thread
}

/// <summary>
/// Unload the splash form
/// </summary>
public void Unload()
{
splash.FadeAway = true; //Triggers the unload
}
}
}


It's called using something like:


Vanilla.FormSplash formSplash = new Vanilla.FormSplash(); //Splash form

//Run the splash form in a separate thread
Vanilla.SplashThreader splashThreader = new Vanilla.SplashThreader(formSplash);

//Simulate initiation
Thread.Sleep(5000);

//Unload the splashForm thread
splashThreader.Unload();


...where FormSplash is any form (usually borderless).

All seems to work perfectly, but I'm a bit nervous about the way it unloads, and also about that loop - although I can't figure another way to do it without rafts of code. Should I be explicitly terminating the thread, rather than leaving it to garbage collection?

This was done in C# VS 2005 Beta 2 by the way, but should run on any C# I think.

Thanks!

conipto
Nov 30th, 2005, 04:57 AM
Only thing that struck me real quick was the while loops and looking at the opacity property. Perhaps that could be better done with for loops...

However, when I've made splash screens in the past, I do it via starting my program from a separate sub main which launches the splash screen form, which kind of has an automatic self-destruct after a time out, then launching the program, using the Application.Run method on each form.

Bill