在不同的线程使用Application.Run() [英] Using Application.Run() on different threads

查看:282
本文介绍了在不同的线程使用Application.Run()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看看下面的代码:

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 );



看起来好像一切工作正常:首先我看到启动画面,后来被MainForm的开始。但不知何故,我得到奇怪的错误,如:图形元素(一个无尽的进度)都显示不正确

修改:我有两个progressbars,一个在启动画面上在MainForm的。他们都表现出endlessmode相同的(错误)的行为:没有进步,只是单纯的背景 /修改

在我看来,这是由于<$ C的召唤$ C> Application.Run()在不同的线程。像,例如

It looks as if all works fine: first I see the splashscreen, later the mainform gets started. But somehow I get strange errors, like: graphical elements (a endless ProgressBar) are not showing up correctly.
EDIT: I have two progressbars, one on the splashscreen, on on the mainform. They both show the same (wrong) behaviour in endlessmode: no progress, just the pure background./EDIT
In my opinion this is due to the call of Application.Run() on different threads. This errors can be eliminated by calling any function/property of the mainForm before starting the splashscreen - like, for instance

mainForm.Text = mainForm.Text;



任何人都可以请确认该代码会引起问题 - 或者说,它应该表现得好吧,我不得不查找错误别的地方?

我已经找了启动画面的实现,我知道它可以做不同。但我有兴趣了解这个执行和可能出现的问题。 !谢谢

Can anyone please confirm that this code can cause problems - or that it should behave alright and I have to look for the error somewhere else?
I already looked for splashscreen implementations and I know that it can be done differently. But I am interested in understanding this implementation and its possible problems. Thanks!

推荐答案

03,我找到了答案:

应用程序。 EnableVisualStyles(); 被称为在构造函数,如果我的MainForm(为什么?)。它在创建任何控件之前被调用。它移动到静态Main()的伎俩。视觉样式都需要无尽的(ProgressBarStyle.Marquee)progressbars。

现在这个闪屏解决方案的作品,因为它应该。

OMG, I found the answer:
Application.EnableVisualStyles(); was called in the ctor if my mainForm (WHY?). It has to be called before any controls are created. Moving it to the static Main() did the trick. The visual styles are required for endless (ProgressBarStyle.Marquee) progressbars.
Now this splashscreen solution works as it should.

这篇关于在不同的线程使用Application.Run()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆