1
votes

I have a .Net Winform application which will take some time to load.

I want to create a Splash Screen which will start when I click on my Application and closes when my Application opens up.Basically through Splash Screen I want to moniter the initialization process of my Application

Splash Screen should be a different winform and should run in separate Thread.

I want to have a button called 'Stop waiting' in Splash screen where user can click on it to cancel opening of the Application and I should be able to shutdown the Application.

Also in the Splash screen I need to show the progress of Initialization of Application.

I have very limited knowledge on Splash Screen.Can you please give me few examples to proceed with my requirement?

One more Question after implementing Splash Screen: My Application Initialization happens in different projects so I have created a seperate exe for Splash Screen and using it in different projects of main Screen.Is it good practice to add splash screen(exe) as reference in Main screen and use methods in Splash Screen?

Thanks in Advance for your help.

2
You can use such solution. Then in the splash screen, it's enough to have a buttons which calls Application.Exit(); when clicked.Reza Aghaei
do you know which part of your code of the main form of your application is taking the long-time to finish?Niklas

2 Answers

2
votes

That's a lot of questions, I'll answer the "how to create a splash screen" and then you can try further with cancelling and showing initialization progress.

You basically create another form which acts as the Splash form.

Set the properties:

  • StartPosition = FormStartPosition.CenterScreen
  • MaximizeBox = false
  • FormBorderStyle = FormBorderStyle.None
  • And load a BackgroundImage if you want

Then in Main before you show your Main Window create the splash screen, show it and pass a reference to the main window object

Splash splash = new Splash();
splash.Show();

MainWindow wnd = new MainWindow(splash);

And inside MainWindowLoad_Load function of you main window:

public MainWindow(Splash splash)
{
  mSplash = splash;
}
private void MainWindow_Load(object sender, System.EventArgs e)
{
  // do long initialisation, and when done....

  mSplash.Close();
  mSplash = null;

}
0
votes

because the splash screen is about the look and feel, i would suggest that you let the UI thread handle it, and take your code into another thread of execution otherwise changing anything in the display of the splash screen requires BeginInvoke. therefore
this is my way of doing it :

private void MainForm_Load(object sender, EventArgs e)
{
    LoadingForm _loading = new LoadingForm();
    BackgroundWorker _worker = new BackgroundWorker { WorkerReportsProgress = true };
    _worker.ProgressChanged += (o, z) =>
    {
        _loading.progressBar1.Minimum = 0;
        _loading.progressBar1.Maximum = 100;
        _loading.progressBar1.Value = (z as ProgressChangedEventArgs).ProgressPercentage;
    };
    _worker.RunWorkerCompleted += (fWorker, fargs) =>
    {
        _loading.Dispose();// this is executed when all the code in dowork event is completed,disposing the loading form and continuing your code
    };
    _worker.DoWork += (workerO, workerArgs) =>
    {
        // execute your code here , update progressbar as you go:
        int yourProgress = 0;//need to generate this in your code as percentage 0-100
        _worker.ReportProgress(yourProgress);


      //////////////////////////just to show progress change//////////////////////////////
        for (int i = 0; i < 5; i++)
        {
            Thread.Sleep(1500);
            yourProgress += 20;
            _worker.ReportProgress(yourProgress);
        }
        Thread.Sleep(3000);
        ////////////////////////////////////////////////////////////////////////////////

    };
    _worker.RunWorkerAsync();



    _loading.ShowDialog(this);


}

note that you need to make your ProgressBar Control's modifiers public,in order to let your backgroundworker change its percentage.