Results 1 to 2 of 2

Thread: Splash form on separate thread

  1. #1

    Thread Starter
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Splash form on separate thread

    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:

    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:

    Code:
    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!
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Splash form on separate thread

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width