启动画面 +主窗口
我有一个 C# 应用程序,当我在加载主窗口之前使用启动屏幕时,当涉及多线程/后台工作人员时,它会遇到麻烦。
我的代码看起来像这样:
[STAThread]
private static void Main()
{
.. do some stuff
ShowSplash(); // where i show a splash screen and load some stuff
...
作为 ShowSplash
的最后一步,我执行以下操作:
new MyCabApplication<MyMainWorkItem, MDIParentForm>().Run(); -- where i load the form through cab.
问题是,当我这样做时,我得到以下异常:
在单个线程上启动第二个消息循环是不是有效的操作。使用 Form.ShowDialog 代替
知道我能做什么吗?
这是我的 showsplash 函数:
private static DialogResult ShowSplash(AutoResetEvent controller)
{
// create and register splash screen
splashScreen = new PointSplashScreen();
Application.Run(splashScreen);
return DialogResult.OK;
}
I have a C# application which runs into trouble when it comes to multi-threads / backgroundworkers when I'm using a splash screen before I load the main window.
my code look something like this:
[STAThread]
private static void Main()
{
.. do some stuff
ShowSplash(); // where i show a splash screen and load some stuff
...
As the last step of ShowSplash
, I do the following:
new MyCabApplication<MyMainWorkItem, MDIParentForm>().Run(); -- where i load the form through cab.
The problem is that when I do that I get the following exception:
Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead
Any idea what can I do?
Here is my showsplash function:
private static DialogResult ShowSplash(AutoResetEvent controller)
{
// create and register splash screen
splashScreen = new PointSplashScreen();
Application.Run(splashScreen);
return DialogResult.OK;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两种解决方案:
Application.Run
,只需创建表单的新实例,然后调用ShowDialog
。调用ShowDialog()
后,将new MyCabApplication().Run();
移至启动屏幕之外。如果不应始终运行此代码,您可以检查启动屏幕的属性。Application.Run(Form)
,而是使用Application.Run(ApplicationContext)
。您将需要创建一个新的ApplicationContext
并将代码移至此处。解决方案 1 更容易。
Two solutions:
Application.Run
, just create a new instance of the form and then callShowDialog
. Movenew MyCabApplication<MyMainWorkItem, MDIParentForm>().Run();
outside of the Splash Screen after the call toShowDialog()
. You can check properties of the Splash screen if this code should not always be run.Application.Run(Form)
, useApplication.Run(ApplicationContext)
. You will need to create a newApplicationContext
and move your code there.Solution 1 is easier.
听起来
MyCabApplication
扩展了 Application。 Run 方法通过启动处理窗口的消息循环来启动 WinForm 应用程序消息。因为您已经在显示 UI,所以已经有一个消息循环正在运行,因此您无法启动另一个消息循环。要显示主窗体,请为其创建一个新实例并调用
Show()
:It sounds like
MyCabApplication
extends Application. The Run method starts a WinForm application by starting a message loop that handles window messages.Because you are already showing UI, there is already a message loop running, so you cannot start another. To get your main form to show up, make a new instance of it and call
Show()
: