【问题标题】:C# Winforms - Splash Screen Hanging on Form_LoadC# Winforms - 在 Form_Load 上挂起的启动画面
【发布时间】:2012-09-11 10:34:03
【问题描述】:

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

【问题讨论】:

标签: c# winforms screen loading splash-screen


【解决方案1】:

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

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

【讨论】:

  • 我确实删除了那个...“DISPOSE”只是我忘记在这里删除的测试...这并不能解决问题...所以我只是显示表格本身在负载上,而不是使用类和线程来使其工作。感谢您的帮助!
  • 如果按照标准,在应用程序启动之前,答案是否定的。我正在尝试显示“加载表单”或“保存屏幕”。我知道我应该改用后台工作人员,但我采取了不同的方式,因为 Thread.Abort() 到处抛出异常:D
猜你喜欢
  • 2017-01-19
  • 1970-01-01
  • 2014-05-10
  • 1970-01-01
  • 2018-03-19
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多