tags:

views:

40

answers:

3

Hi,

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

+1  A: 

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.

ChrisF
A: 

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;
    }
Daniel Mošmondor