请看一下下面的代码:
var splashForm = new SplashForm();
m_Thread = new Thread( () => System.Windows.Forms.Application.Run( splashForm ) )
m_Thread.Start();
// Do some initialization
// ...
// the following method just invokes `Close()` on the right thread
splashForm.Shutdown();
// Loop until the thread is no longer alive
// ...
System.Windows.Forms.Application.Run( mainForm );
看起来好像一切正常:首先我看到启动画面,然后主窗体启动。但不知何故,我遇到了奇怪的错误,例如:图形元素(无尽的进度条)未正确显示。
EDIT:我有两个进度条,一个在启动屏幕上,一个在主窗体上。它们在无尽模式下都表现出相同(错误)的行为:没有进展,只有纯粹的背景。/EDIT
在我看来,这是由于Application.Run()
在不同的线程上。可以通过在启动启动画面之前调用 mainForm 的任何函数/属性来消除此错误 - 例如
mainForm.Text = mainForm.Text;
任何人都可以确认此代码可能会导致问题 - 或者它应该表现良好,而我必须在其他地方查找错误?
我已经在寻找闪屏实现,并且我知道它可以以不同的方式完成。但我有兴趣了解这个实现及其可能的问题。谢谢!
SplashForm 显示的线程需要有一个 Windows 消息泵,以便处理每个窗口/控件产生的消息。为此,您需要使线程成为 STA 线程。尝试打电话SetApartmentState
before启动线程
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)