QANew
QANew

Reputation: 52

Better way of doing Splash Screen

at the moment I got this

UPDATED: Thanks for all the answers.

 private void Form1_Load(object sender, EventArgs e)
    {
        //hide() doesnt help
        Thread init = new Thread(InitApplication);
        init.IsBackground = true;
        init.Start();
    }

InitApplication takes at least 5+ seconds to complete and write in all the settings. I want my splashScreen to stay up until then.

ADDED:

    private readonly SplashScreen _splash;
    public Form1(SplashScreen splashy)
    {
        _splash = splashy;
        InitializeComponent();
    }

and I got

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        SplashScreen splashy = new SplashScreen();
        splashy.Show();
        Application.DoEvents();
        Application.Run(new Form1(splashy));
    }

It is doing what its suppose to do, However, Now I see form1 on top of the splashform. Where can I add the hide and show method so it only the splash screen is shown and form1 popups when its fully loaded.

Upvotes: 0

Views: 1194

Answers (2)

Sarvesh Mishra
Sarvesh Mishra

Reputation: 2072

You can use Async Await, Background worker or Thread for initializing your app etc. These are written in the sequence of easy to use and the general pattern being followed.

Use a normal windows form and there use progress bar, gif image etc. what you like most there. On the form load or form shown event start the background task and when it finishes, close the normal form and load your main application form.

If your InitApplication has any call that deals with GUI, you will have to change that call and Invoke the desired action on GUI thread.

Upvotes: 1

khargoosh
khargoosh

Reputation: 1520

Try loading the splash screen from the UI thread, then use a background worker to execute InitApplication(). The background worker can communicate progress to the UI thread.

BackgroundWorker Class on MSDN

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

Upvotes: 1

Related Questions