MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to make a Splash Screen in CSharp

The tutorial aims to cover the process of coming up with a Splash Screen making use of C#.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Lot many times an application is required to do many time consuming operations at start-up. Sometimes you need to read data from a database, sometimes you may need to retrieve some data from a web service. When this happens, it's often useful to display a "Splash Screen" to the user, with a company logo or something, along with a progress bar to indicate how much longer it will take for the app to load. While it may sound simple at first, it can be a bit tricky; if you simply show a screen, and do your time consuming operations, your UI will hang and your progress bar will never update. Therefore, there's some threading involve, so we will demonstrate a simple example here.

Start off by creating a simple Windows Forms project. Once it's loaded, add another window for and call it "SplashScreen". To get the look and feel right, let's set some properties:

  • FormBorderStyle: None
  • TopMost : True
  • StartPosition: CenterScreen

Now, in the properties window, find the BackgroundImage property and click the little elipsis {...} and select a picture from your hard drive. The BackgroundImageLayout property is changed to None but you can do whatever you want. Then, add a progress bar to the bottom of the form, and set the Dock property to Bottom. Here's what our splash screen looks like in the designer:

Splash Screen

Figure 1: Splash Screen

Now, we need to give access to someone outside of this class to update the progress (the progressBar is a private member and can't be accessed.) Here's the problem; if we simply wrap the progress bar's Value property in our own getter/setter like this:

Listing1: Code displaying the wrapping of progress bar’s Value Property

    Public int Progress
      {
          get
          {
              return this.progressBar1.Value;
          }
          set
          {
              this.progressBar1.Value = value;
          }
      }

While you can do that, remember, this splash screen will be shown in a separate thread. If you then try to access this property from your main thread, you'll get an InvalidOperationException that "Cross-thread operation not valid:

Control 'progressBar1' accessed from a thread other than the thread it was created on." So, in order to be able to set any of the UI elements from another thread, we need to call the form's Invoke method which takes a delegate.

Here's the complete code for the SplashScreen class:

Listing2: Code displaying the SplashScreenclass

using System.Windows.Forms;

namespace SplashScreenTesting
{
   public partial class SplashScreen : Form
   {
       private delegate void ProgressDelegate(int progress);

       private ProgressDelegate del;
       public SplashScreen()
       {
           InitializeComponent();
           this.progressBar1.Maximum = 100;
           del = this.UpdateProgressInternal;
       }

       private void UpdateProgressInternal(int progress)
       {
           if (this.Handle == null)
           {
               return;
           }
           this.progressBar1.Value = progress;
       }
       public void UpdateProgress(int progress)
       {
           this.Invoke(del, progress);
       }
   }
}

As you can see, we created a delegate that we'll use to invoke the update to the progress bar. Let's create a class that simulates a time consuming operation. Basically, it just calculates Math.Pow for numbers 1 - 100 raised to the 1 - 500,000th power. Every time we move on to another outer number (the 1 - 100) we raise an event that reports progress. Once it's done, we raise an event that we're done. Here's the complete class:

Listing3: Code displaying the complete class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SplashScreenTesting
{
 public class Hardworker
 {
     public event EventHandler<HardWorkerEventArgs> ProgressChanged;
     public event EventHandler HardWorkDone;
     public void DoHardWork()
     {
         for (int i = 1; i <= 100; i++)
         {
             for (int j = 1; j <= 500000; j++)
             {
                 Math.Pow(i, j);
             }
             this.OnProgressChanged(i);
         }
         this.OnHardWorkDone();
     }
     private void OnProgressChanged(int progress)
     {
         var handler = this.ProgressChanged;
         if (handler != null)
         {
             handler(this, new HardWorkerEventArgs(progress));
         }
     }
     private void OnHardWorkDone()
     {
         var handler = this.HardWorkDone;
         if (handler != null)
         {
             handler(this, EventArgs.Empty);
         }
     }
 }
 public class HardWorkerEventArgs : EventArgs
 {
     public HardWorkerEventArgs(int progress)
     {
         this.Progress = progress;
     }
     public int Progress
     {
         get;
         private set;
     }
 }
}

Now, on to the main part of the app; the displaying of the splash screen: In the Form1's load event, we'll spawn off another thread to actually display the splash screen and then on the main thread we'll do our "Hard Work". Once the HardWorker has reported that the progress is complete, we'll dispose of the splashscreen and display the main Form.

Listing 4: Code to display the splashing screen

using System;
using System.Windows.Forms;
using System.Threading;
namespace SplashScreenTesting
{
public partial class Form1 : Form
{
    private SplashScreen splashScreen;
    private bool done = false;
    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(HandleFormLoad);
        this.splashScreen = new SplashScreen();
    }
    private void HandleFormLoad(object sender, EventArgs e)
    {
        this.Hide();
        Thread thread = new Thread(new ThreadStart(this.ShowSplashScreen));
        thread.Start();
        Hardworker worker = new Hardworker();
        worker.ProgressChanged += (o, ex) =>
        {
            this.splashScreen.UpdateProgress(ex.Progress);
        };
        worker.HardWorkDone += (o, ex) =>
        {
            done = true;
            this.Show();
        };
        worker.DoHardWork();
    }
    private void ShowSplashScreen()
    {
        splashScreen.Show();
        while (!done)
        {
            Application.DoEvents();
        }
        splashScreen.Close();
        this.splashScreen.Dispose();
    }
}
}

In the constructor we just hook into the Load event and new up the SplashScreen. Then, in the Load event handler, we first hide the current form, because we don't want that to be seen just yet. We then create a Thread and pass in a delegate to our ShowSplashScreen method. The show splash screen method is what's actually going to be run on a seperate thread. First, it displays the SplashScreen. Then, it just sits there in a constant loop waiting for the "done" bool to be set to true. The key ingredient here is the call to Application.Doevents().

When you run a Windows Form, it creates the new form, which then waits for events to handle. Each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue. While your code handles the event, your application does not respond. For example, the window does not repaint if another window is dragged on top.

If you call DoEvents in your code, your application can handle the other events. For example, if you have a form that adds data to aListBox and add DoEvents to your code, your form repaints when another window is dragged over it. If you remove DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing.

Basically, it allows other events to be taken care of, even though the current procedure is blocking execution. This allows our progress bar to be updated.

Back in the form load, we then new up our HardWorker and hook into it's events. Basically, every time the HardWorker reports progress, we update our progress bar. Then when it's done, we set our done flag to true, and show the main form. Finally, we actually kick it off with a call to DoHardWork();

Conclusion

Try it out and run it. It's actually kind neat to see it in action. This is obviously a very rough example, but it should give you a basic idea on how to get this done. Hope you enjoyed reading the tutorial.



Software Developer from India. I hold Master in Computer Applications Degree and is well versed with programming languages such as Java, .Net, C and C++ and possess good working knowledge on Mobile Platforms as well.

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login