How to Do application initialization while showing a splashscreen

Source: Internet
Author: User
Document directory
  • Preparations
  • 1. Moving the applications entry procedure
  • 2. Creating the splash screen
  • 3. Modifying the mainform's code
  • 4. Modifying the apploader class
  • Preparations
  • 1. Modify the splashform
  • 2. Creating the splasher class
  • Modifying the apploader class

The article I see on the Internet is detailed.

How to Do application initialization while showing a splashscreen By Matthias S..

Update 28.01.03

This article has been updated with Chris Austin's suggestion (Thanks Chris! And thanks to Phil Bolduc for the fix on Chris 'stuff;) and an alternative (multithreaded) approach has been added, based on jabes idea (thanks jabes !).

Introduction

This articles shows how to display a splash screen during your application initialization. This article assumes that you familiar with C # And the Vs. Net IDE.

Background

I had put the lot of my initialization code into the main formsOnLoad()Override. the Code parsed configuration files, created control and the such. when my application started up, it looked like a mess: the main application form just didn't pop up nicely and ready to be used. I decided to create a splash screen, which shocould pop up right after starting the executable. while the splash screen was shown, I wanted to do my initialization and once that was done, I 'd hide the splash screen and show the main application window. this article contains various approaches on how it can be realized.

Version A: quick 'n' dirty

This version was the quick and dirty solution I came up with. Credits to Chris Austin for suggesting the usage ofApplicationContextClass.

Preparations
  1. Create a new empty Windows Forms project.
  2. Add a new form (for your splash screen and name it splashform ).
1. Moving the applications entry procedure

This is not required, but helps a beginning developer understand thatMain()Entry point and the form1 class into which it is put by default are not necessarily coupled. so let's create a new class called apploader and put the entry point in there. once done, the class shoshould look like this:

public class AppLoader{public AppLoader(){}[STAThread]static void Main(){}}

Once completed, remove the code from your form1.cs so that you don't end up having 2 entry points.

2. Creating the splash screen

Just create a new windows form, place a picturebox on it and set a couple of properties just to make it look nicer. The form shocould be centered on the screen (StartPosition), Be topmost (TopMost) And shouldn't have a border (FormBorderStyle). As for the picturebox, setDockProperty to fill and setImageProperty to your splash image. that's it. in real life, you might want to write some code to display the application name, it's version, the registered user and a copyright notice, etc.

3. Modifying the mainform's code

Now we need to do some modifications to the form1 class. since some initialization code can be put into the class Constructor (such as reading files, etc .) some can't (like creating controls) sincethisIs not available in the constructor. So you can't for example add controls to a formsControlsCollection. Lets Add a new public method (e.g.public void PreLoad(){...}). This function shoshould look somewhat like this:

public void PreLoad(){if (_Loaded){//just return. this code can't execute twice!return;}//do your initialization here//...//flag that we have loaded all we need._Loaded = true;}

The_LoadedVariable is a private bool, which shocould be initializedfalse. You definitely shocould check this flag in your main formsOnLoad()Override (which is called just before the form is shown the first time). For example:

protected override void OnLoad(System.EventArgs e){if (!_Loaded){//good idea to throw an exception here.         //    the form shouldn't be shown w/o being initialized!return;}}
4. Modifying the apploader class

Here is the code forAppLoaderClass. See the comments for information.

Collapse
public class AppLoader{private static ApplicationContext context;private static SplashForm sForm = new SplashForm();private static MainForm mForm  = new MainForm();[STAThread]static void Main(string[] args){//first we retrieve an application context for usage in the         //    OnAppIdle event handlercontext = new ApplicationContext();//then we subscribe to the OnAppIdle event...Application.Idle += new EventHandler(OnAppIdle);//...and show our SplashFormsForm.Show();//instead of running a window, we use the contextApplication.Run(context);}private static void OnAppIdle(object sender, EventArgs e){if(context.MainForm == null){//first we remove the eventhandlerApplication.Idle -= new EventHandler(OnAppIdle);//here we preload our formmForm.PreLoad();//now we set the main form for the context...context.MainForm = mForm;//...show it...context.MainForm.Show();//...and hide the splashscreen. done!sForm.Close();sForm = null;}}}

Version B: multithreaded

This version entirely based on jabes idea. For more information read the messages below. Thanks again to jabes for letting me use his stuff in this article!

In this version, the splashform is shown in a separate thread and displays the current loading status (like in Photoshop ). you will want to multi-thread your splash screen if it is going to be on-screen for any length of time; by running the splash screen from its own message pump, screen redraws and other Windows messages are processed correctly giving a more professional impression.

Preparations
  1. Create a new empty Windows Forms project.
  2. Add a new form (for your splash screen and name it splashform ).
  3. Repeat the steps from the previous section which create the apploader class containing the entry point for the application. Leave the method bodyMain()Empty for now. Don't forget to removeMain()Method from the form1 code!
1. Modify the splashform

In order to display the loading status on the form, we need to place a label on the form. for our example we call it lstatusinfo. additionally we create a GET/set property named statusinfo. after that we introduce a private string member called _ statusinfo which maps directly to the property. the property implementation shoshould look like this:

public string StatusInfo {set {_StatusInfo = value;ChangeStatusText();}get {return _StatusInfo;}}

ChangeStatusText()Is a helper function which will be called each time the statusinfo shocould be updated. It's implementation shoshould look like this:

public void ChangeStatusText() {try {if (this.InvokeRequired) {this.Invoke(new MethodInvoker(this.ChangeStatusText));return;}lStatusInfo.Text = _StatusInfo;}catch (Exception e) {//do something here...}}

The check forInvokeRequiredProperty is necessary because controls are not thread safe. so if you call a method on a control from a different thread (then the one which created the control) without calls alling the call into the proper thread, weird things might happen.

That's it. Let's create the splasher class which maintains thread creation, start and stop.

2. Creating the splasher class

The static membersShow(),Close()AndStatusProperty can be used as their names suggest to show and close the splashwindow as well as to update the loading status displayed on the splashform (usingStatusInfoProperty previusly created ).

Here is what the implementation of the class looks like:

Collapse
public class Splasher{static SplashForm MySplashForm = null;static Thread MySplashThread = null;//internally used as a thread function - showing the form and//starting the messageloop for itstatic void ShowThread(){MySplashForm = new SplashForm();Application.Run(MySplashForm);}//public Method to show the SplashFormstatic public void Show(){if (MySplashThread != null)return;MySplashThread = new Thread(new ThreadStart(Splasher.ShowThread));MySplashThread.IsBackground = true;MySplashThread.ApartmentState = ApartmentState.STA;MySplashThread.Start();}//public Method to hide the SplashFormstatic public void Close(){if (MySplashThread == null) return;if (MySplashForm == null) return;try{MySplashForm.Invoke(new MethodInvoker(MySplashForm.Close));}catch (Exception){}MySplashThread = null;MySplashForm = null;}//public Method to set or get the loading Statusstatic public string Status{set{if (MySplashForm == null){return;}MySplashForm.StatusInfo = value;}get{if (MySplashForm == null){throw new InvalidOperationException("Splash Form not on screen");}return MySplashForm.StatusInfo;}}}

OK, let's look at what we have here:

TheShow()Method will create a new thread using the staticShowThreadFunction as a target. This function will simply show the form and start a messageloop for it.Close()Method will again marshal a call to the splashformsClose()Method into the appropriate thread thus causing the form to close.StatusProperty can be used to retrieve or update the statusinfo displayed on the splashscreen.

Here are some simple examples of their usage:

...//note that there is no instance required since those functions are staticSplasher.Show();//shows the SplashScreen        //sets the status info displayed on the SplashScreenSplasher.Status = "Initializing Database Connection...";Splasher.Close();//closes the SplashScreen...
Modifying the apploader class

In order to use the splasher class we have to modify our apploader class appropriately:

public class AppLoader{public AppLoader(){}[STAThread]static void Main(string[] args){Splasher.Show();DoStartup(args);Splasher.Close();}static void DoStartup(string[] args){//do whatever you need to doForm1 f = new Form1();Application.Run(f);}}

First we showSplashFormAnd set the initialStatusInfoText. Then we callDoStartup(), A good place to put any initialization code which doesn't require a form to be created.

Why moving the startup code into an extra function you might ask. the answer is simple: Since the jitter works on a per function basis, it won't need to do too much code before your splash-screen is actually getting shown on the screen.

To ensure that after you main application form is loaded, it actually gets focused; you might want to put the following statement into the form1OnLoadOverride:

this.Activate();
Outroduction

Well, that's it. No expensive wisdom here but hope that it'll help you getting started. once again thanks to the people the helped improving this article:

  • Jabes
  • Chris Austin
  • Phil Bolduc
History
27.01.03 Updated with an alternative approach (multithreaded) to solve this problem. plus additional improvements to the existing sample code.
22.01.03 Initial publication.


About Matthias S.

Doesn' t hurt animals. pretty good at playing bass. listens to tool, Primus, memory in Dvorak and sometimes dream theater. prefers beer over wine and jeans over suit. has read Jon littmans 'the fugitive game' lately and wonders how Kevin Poulsen endet up as an editor for wired.com. has never fired a gun and probably never will. plays table-soccer once a week at a minimum. and yes, he does code as well. mostly C # and Internet applications these days.

Click here to view Matthias S.'s online profile.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.