多线程 GUI 应用程序的设计模式
我正在开发一个多线程 GUI 应用程序,GUI 更改取决于来自另一个通信线程的数据。
通信线程设置GUI的数据和数据变化的标志。
目前,我们正在主线程中的计时器的帮助下检查网络线程设置的标志以及更改 GUI 的情况。
但是由于项目规模太大,它变得混乱。
因此,如果有任何设计模式可以解决此类问题...
提前致谢...
I am working on a multithreded GUI application GUI change depends on the data coming from another communcation thread.
communication thread sets data for GUI and flags for change in data.
Currently we are checking with the help of a timer in the main thread for flags set by the network thread and on change change GUI also.
But it is getting messy due to large size of project.
So if there any design pattern for these kind of problems...
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我参加了 Emergent Design 的作者 Scott L. Bain 的讲座,他在演讲中描述了使用 Mediator 模式 作为 GUI 和业务逻辑之间的中介。在他的示例中,他们能够将常规 GUI 与(根据记忆解释)针对残障人士的自然语言输入进行交换,而无需更改业务逻辑。类似的东西可以帮助您将线程生成的数据同步到 GUI,同时分离类的职责。
I attended a lecture by Scott L. Bain author of Emergent Design where he described using the Mediator pattern for an intermediary between gui and business logic. In his example they were able to exchange a regular gui with (paraphrasing from memory) a natural language input for handicapped folks, without changing the business logic. Something similar may help you synchronize the data produced from your thread to your gui, while separating the responsibility of your classes.
如何使其成为事件驱动的? GUI 层订阅来自数据或通信层的事件,并且当事件被触发时,它知道要更新自身。通过这种方式,GUI 层了解数据/通信层,但反之则不然。
顺便说一句,不确定您的环境,但必须小心地从触发事件的线程更新 GUI。例如,在 .NET 中,您需要使用 Control.Invoke (http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx) 从非 GUI 线程更新 GUI。
How about making it event driven? The GUI layer subscribes to events from the data or communication layer and when the event is fired, knows to update itself. In this way the GUI layer knows about the data/comms layer but not vice versa.
Btw, not sure your environment, but one must be careful updating the GUI from the thread the event is fired on. In .NET for instance, you would need to use Control.Invoke (http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx) to update the GUI from a non-GUI thread.