Source: http://www.codeproject.com/Articles/3542/How-to-do-Application-Initialization-while-showing? Rp =/KB/CS/apploadingarticle/apploading1.zip
- Version A (quick 'n' dirty): Download Demo project-32 KB
- Version B (multithreaded): Download Demo project-32 KB
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
- Create a new empty Windows Forms project.
- 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.
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
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
- Create a new empty Windows Forms project.
- Add a new form (for your splash screen and name it splashform ).
- Repeat the steps from the previous section which create the apploader class containing the entry point for the application. Leave the method body
Main()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 authentication 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()And
TheStatusProperty 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 (using
TheStatusInfoProperty previusly created ).
Here is what the implementation of the class looks like:
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();