且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用启动画面正确加载表单?

更新时间:2023-01-25 23:21:47

谢谢!

这个问题很容易看清(可能很难解决)
Thank you!

The problem is pretty simple to see (it may be harder to work out a fix)
timer1.Stop(); // Before calling f2.ShowDialog(), stops the timer event
Hide();
f3.ShowDialog();
Close();

ShowDialog是一个模式调用或阻塞调用-在关闭调用它的窗体之前,它不会返回.因此,在Form2实例(启动屏幕)完成其工作并关闭之前,Form2类中不会再发生任何事件.这样就不会显示进度,也不会显示消息,也根本不会更新UI.

取而代之的是,为Form3.Closed事件添加一个处理程序,并使用该处理程序关闭Form2.只需使用f3.Show()而不是f3.ShowDialog()并在其后删除Close-如果在显示form3之后关闭form2,则由于它被声明为form2类级别引用,它也会破坏form3实例.

ShowDialog is a modal or blocking call - it does not return until the form it is called on is closed. So no further events happen in your Form2 class until the instance of Form3 (the splash screen) has done it''s job, and closed. So not progress display, not messages, no UI updates at all.

Instead of that, add a handler for the Form3.Closed event, and use that to Close Form2. Just use f3.Show() instead of f3.ShowDialog() and remove the Close after it - if you close form2 after displaying form3, it will also destroy the form3 instance, since it is declared as a form2 class level reference.