GurdeepS
GurdeepS

Reputation: 67293

Making a winforms 2.0 Splash Screen

What is the easiest way to fire a splash screen (Which disappears on its own) in a C# / .NET 2.0 winforms app? It looks like the VisualBasic assembly (which can be called from C# nonetheless) has a way to do this, but are there any simple examples?

Thanks

Upvotes: 3

Views: 2849

Answers (2)

Daniel Mošmondor
Daniel Mošmondor

Reputation: 19986

Easiest way would be to create a form and allow it to kill itself after some time it is shown. But, things gets more complicated if you want this form to be able to display some application loading progress while application is initializing, and disappear for example 3 seconds after application is really ready for use.

Idea would include putting the splash screen on completely different thread from the main application. It's thread function should go like that:

    static void ThreadFunc()
    {
        _splash = new Splash();
        _splash.Show();
        while (!_shouldClose)
        {
            Application.DoEvents();
            Thread.Sleep(100);
            if (new Random().Next(1000) < 10)
            {
                _splash.Invoke(new MethodInvoker(_splash.RandomizeText));
            }
        }
        for (int n = 0; n < 18; n++)
        {
            Application.DoEvents();
            Thread.Sleep(60);
        }
        if (_splash != null)
        {
            _splash.Close();
            _splash = null;
        }
    }

Then, you can use this to show and hide it:

    static public void ShowSplash()
    {
        _shouldClose = false;
        Thread t = new Thread(ThreadFunc);
        t.Priority = ThreadPriority.Lowest;
        t.Start();
    }
    internal static void RemoveSplash()
    {
        _shouldClose = true;
    }

Upvotes: 0

ChrisF
ChrisF

Reputation: 137198

There's a detailed tutorial on Code Project which puts the splash screen on its own thread so the main app can get on with loading up.

Upvotes: 1

Related Questions