Load data using SplashScreen

Source: Internet
Author: User

Background
For windows developers, when opening the VS development tool, they always first present a SplashScreen interface, and only a few seconds before opening the main interface of. This effect is generally because a large amount of resources need to be loaded on the main interface. To prevent the main interface from becoming "dead", a friendly Loading screen is provided. To achieve this, we usually open a SplashScreen window before loading the main interface Application. Run (new MainForm () and load data in the SplashScreen window.

Interface WindowsFormsApplicationBase
Microsoft provides the WindowsFormsApplicationBase class, which provides the SplashScreen attribute and the interface of the OnCreateSplashScreen virtual method. When implementing your own SplashScreen window, we mainly reload the OnCreateSplashScreen method, create a Form object, assign values to the SplashScreen attribute, and this class also provides the MinimumSplashScreenDisplayTime attribute to set the display time of the SplashScreen window. Of course, you can control the rendering and closing of the SplashScreen window by yourself.

Implementation Application class
First, we need to implement the WindowsFormsApplicationBase class SplashScreenApplication and re-use the OnCreateSplashScreen method. The Code is as follows:

Internal sealed class SplashScreenApplication: WindowsFormsApplicationBase {
Public SplashScreenApplication (){
Base. IsSingleInstance = true;
Base. EnableVisualStyles = true;
Base. MinimumSplashScreenDisplayTime = 2000;
}

Protected override void OnCreateMainForm (){
This. MainForm = new Form1 ();
}

Protected override void OnCreateSplashScreen (){
This. SplashScreen = new SplashScreenForm ();
}
}
The Form1 is the main window, And the SplashScreenForm is the Loading window. The SplashScreen is automatically disabled in 2000 milliseconds, and an attempt is made to open the main form. The Application will first execute the OnCreateSplashScreen method, and then execute the OnCreateMainForm window. It should be noted that 2000 milliseconds is not the interval between two methods. Instead, the SplashScreen form is closed after the main form is created for 2000 milliseconds and the main form is displayed. The SplashScreenForm here cannot be used to load data, because it will be disabled after 2000 milliseconds, we cannot ensure that the SplashScreen can be loaded in 2000 milliseconds.

Load results
The purpose of the SplashScreen is to load data, rather than simple and friendly effects. Therefore, a simple 2000 ms cannot meet our data loading needs. In view of this, we need to control the SplashScreen interface by ourselves. After the data is loaded, we can close the SplashScreen window and display the main interface. To load data, the SpashScreen interface displays the data loading process. The Code is as follows:

Internal sealed class SplashScreenApplication: WindowsFormsApplicationBase {
Public ManualResetEvent resetEvent = new ManualResetEvent (true );

Public SplashScreenApplication (){
Base. IsSingleInstance = true;
Base. EnableVisualStyles = true;
}

Protected override void OnCreateMainForm (){
If (resetEvent. WaitOne ()){
This. MainForm = new Form1 ();
}
}

Protected override void OnCreateSplashScreen (){
This. SplashScreen = new SplashScreenForm ();
}
}
The WindowsFormsApplicationBase class is located in the Microsoft. VisualBasic. ApplicationServices namespace and must be referenced by Microsoft. VisualBasic. dll. To demonstrate the loading process, SplashScreenForm is also responsible for loading data:

Public partial class SplashScreenForm: Form {
Static string [] Resources = new string [] {
"Check update .",
"Updating ",
"Downloading xxx. dll from remote server .",
"Downloading xxx. config from remote server .",
"Updated .",
"Check if the devices pluged in .",
"Open devices .",
"Load data from database .",
"Load file data from remote .",
"Download necessary files .",
"Ready"
};

Public SplashScreenForm (){
InitializeComponent ();
This. Opacity = 0.8;
Program. app. resetEvent. Reset ();
}

Private void SplashScreenForm_Load (object sender, EventArgs e ){
BackgroundWorker1.RunWorkerAsync ();
}

Private delegate void run ();

Private void backgroundworker=dowork (object sender, DoWorkEventArgs e ){
Float total = 0f;
Float pre = 100f/Resources. Length;

Foreach (string res in Resources ){
Total + = pre;
BackgroundWorker1.ReportProgress (int) total, res );
Thread. Sleep (1000 );
}
}

Private void backgroundWorker1_ProgressChanged (object sender, ProgressChangedEventArgs e ){
This. Invoke (new run () => {
Label1.Text = e. UserState. ToString ();
ProgressBar1.Value = e. ProgressPercentage;
}));
}

Private void backgroundworkerappsrunworkercompleted (object sender, RunWorkerCompletedEventArgs e ){
This. Invoke (new run () => {
Label1.Text = "Inititalization finished .";
ProgressBar1.Value = 100;

}));

Thread. Sleep (1000 );

This. Invoke (new run () => {
This. Close ();
Program. app. resetEvent. Set ();
}));
}
}
Program entry:
Static class Program {
Public static SplashScreenApplication app = new SingleInstanceApplication ();
/// <Summary>
/// The main entry point for the application.
/// </Summary>
[STAThread]
Static void Main (string [] args ){
App. Run (args );
}
}
Running Effect
The running effect is as follows:

 

Others
In addition, the data loading process is only in the SplashScreen window, so the MainForm cannot load resources that are too time-consuming. Otherwise, after the SplashScreen window is closed, the MainForm is created, it won't have the effect of Loading. If you want to load data in MainForm, you can also modify the code to disable SplashScreen in MainForm.

Related Article

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.