简体   繁体   中英

C# Winforms - Splash Screen Hanging on Form_Load

OK guys, I have this Class that shows a "Loading..." Splash Screen. It works great when I call it on Initialize() but not on Form_Load. Instead of showing at the beginning of Form_Load, it shows after all tables are filled and then just hangs there (no lock).

class innerLoad
{
    //Delegate for cross thread call to close
    private delegate void CloseDelegate();


    //The type of form to be displayed as the splash screen.
    private static frmLoading splashForm;

    static public void ShowSplashScreen()
    {
        // Make sure it is only launched once.
        if (splashForm != null)
            return;

        Thread thread = new Thread(new ThreadStart(innerLoad.ShowForm));
        thread.IsBackground = true;
        //Thread.Sleep(100);
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();


    }
    //volatile static public bool isOpen = false;
    static private void ShowForm()
    {

        splashForm = new frmLoading();

        splashForm.ShowDialog();
        splashForm.Dispose();
    }

    static public void CloseForm()
    {
        try
        {
            if (splashForm == null)
                return;
            splashForm.Invoke(new CloseDelegate(innerLoad.CloseFormInternal));
        }
        catch
        {

        }

    }

    static private void CloseFormInternal()
    {
        splashForm.Close();
        splashForm = null;
    }


}

And here is the Form_Load Code:

 private void frmPayGen_Load(object sender, EventArgs e)
    {
        //th1 = new Thread(LoadingForm);
        //th1.Start();
        //Thread.Sleep(500);
        innerLoad.ShowSplashScreen();
        fill();
        innerLoad.CloseForm();

        //Thread.Sleep(500);
    }

I appreciate your help and I love this site... helps me a lot :D

If you set a breakpoint at the start of your Form Load event, and use F11 to step through, you eventually see this exception:

错误

Exceptions in a Form Load event are basically ignored. If an exception is thrown, nothing after the line where the exception was thrown runs, but the Windows Form doesn't crash either. Taking away this line of code should make things work as you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM