0

好的,伙计们,我有一个显示“正在加载...”启动画面的类。当我在 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

4

1 回答 1

0

如果您在表单加载事件开始时设置断点,并使用 F11 单步执行,您最终会看到以下异常:

错误

表单加载事件中的异常基本上会被忽略。如果抛出异常,则抛出异常的行之后不会运行,但 Windows 窗体也不会崩溃。去掉这行代码应该会让事情如你所愿。

于 2012-09-11T10:49:37.173 回答