C#Winforms - 悬挂在Form_Load上的启动画面

时间:2012-09-11 10:34:04

标签: c# winforms screen loading splash

好的伙计们,我有这个类,显示“正在加载......”启动画面。当我在Initialize()上调用它而不是在Form_Load上调用它时,它工作得很好。它不是在Form_Load的开头显示,而是在所有表格填满后显示,然后挂起(没有锁定)。

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;
    }


}

这是Form_Load代码:

 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);
    }

感谢您的帮助,我喜欢这个网站...对我很有帮助:D

1 个答案:

答案 0 :(得分:0)

如果在Form Load事件开始时设置断点,并使用F11单步执行,则最终会看到此异常:

Error

表单加载事件中的异常基本上被忽略。如果抛出异常,则抛出异常行之后的任何内容都不会运行,但Windows窗体也不会崩溃。拿掉这行代码应该可以让你的工作正常工作。

相关问题