The5thBluesky
The5thBluesky

Reputation: 45

C# Splash Screen won´t display

I´m programming an installer that installs new software and so on and, in addition, I want to read some hardware stuff like CPU architecture and so on. But this takes a while, so I want to display a splash screen while the program reads the hardware. But it won´t display and I don´t know why.

Can someone help me, please?

In the Header of the Mainclass I create a new Thread:

public Hauptfenster()
        {
            InitializeComponent();
            t = new Thread(new ThreadStart(ZeigeLadeBildschirm));
            t.Start();
        }

When the Thread starts the SplashScreen should be displayed and the hardware should be read by the Mainclass. So the method "ZeigeBildschirm" says:

public void ZeigeLadeBildschirm()
    {
        SplashScreen sp = new SplashScreen();

        sp.Show();
        createFolder();
        entpacken();
        Verionszahlalsd();
        GetHardwareInformationRAM();
        GetOSInformation();
        GetReleaseDateBIOS();
        GetLastBootTime();
        GetHardwareInfoVideo();
        GetHardwareInfoMonitor();
        GetSystemInfo();
        GetCDDriveInformation();
        GetDiskDriveInformation();
        GetHardwareInfoCPU();
        GetInstallDateWindows();

    }

to stop the Mainthread while the new Thread reads out the hardware I coded this after creation of the Thread in the Header:

while (t.IsAlive)
    {
    }

So the Header looks like this:

public Hauptfenster()
        {
            InitializeComponent();
            Initialisierung();
            t = new Thread(new ThreadStart(ZeigeLadeBildschirm));
            t.Start();
            while (t.IsAlive)
            {
            }
            GetAdditionRAMInfo();
            SchreibeDaten();
        }

But when I start the program, the hardware is read out but no SplashScreen is displayed on the screen. After the hardware reading code is finished, the MainForm opens and shows all data. How can I fix that code, so that the SplashScreen will be displayed, the hardware will be read out and the SplashScreen disappears and the MeinForm will be displayed?

Code of SplashScreen:

namespace The5thBlueskyInstallerSplashScreen
{
    public partial class SplashScreen : Form  

    {

        public SplashScreen()
        {
            InitializeComponent();
        }
    }
}

Upvotes: 1

Views: 103

Answers (1)

Mark Hurd
Mark Hurd

Reputation: 10931

You need to do all UI on the main thread.

Just shift the splash screen creation and showing to the main class, before creating the thread, and close it, from the main thread, after the extra thread completes.

It would be preferable to use events, but, with my suggestion in comments to use DoEvents, a minimal (untested) change would be:

public Hauptfenster()
    {
        InitializeComponent();
        Initialisierung();
        SplashScreen sp = new SplashScreen();
        sp.Show();
        t = new Thread(new ThreadStart(ZeigeLadeBildschirm));
        t.Start();
        while (t.IsAlive)
        {
            System.Windows.Forms.Application.DoEvents();
            System.Windows.Forms.Application.DoEvents();
            Thread.Sleep(55);
        }
        GetAdditionRAMInfo();
        SchreibeDaten();
        sp.Close();
    }

And remove the sp references in the other thread.

Upvotes: 1

Related Questions