同一窗口上有多个UI线程 [英] Multiple UI threads on the same window

查看:201
本文介绍了同一窗口上有多个UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不需要多个窗口,每个窗口都有自己的UI线程,也不希望在单个UI线程上引发事件,也不需要后台工作程序和通知,也不需要Invoke和BeginInvoke东西.

I don't want multiple windows, each with its own UI thread, nor events raised on a single UI thread, not background workers and notifications, none of that Invoke, BeginInvoke stuff either.

  • 我对允许多个线程以安全方式更新同一窗口的平台感兴趣.类似于第一个线程创建三个按钮,第二个线程创建另外五个按钮,它们都可以访问它们,更改其属性并删除它们,而不会产生任何不良后果.
  • 我希望在不调用UI的情况下安全地对UI进行多线程访问,在该平台上,可以直接从任何线程访问UI对象,而不会出现诸如只能从创建对象的线程访问对象"之类的错误.为了让我可以根据需要进行同步,请不要阻止我以跨方式直接访问UI.

推荐答案

您可以创建线程安全的委托人生产者/消费者队列. 任何想要更新UI组件的线程都会创建一个封装要执行的操作的委托,并将其添加到队列中. 然后,UI线程(假设所有组件都在同一线程上创建)将定期从队列中拉出一个项目,并执行委托.

You could make a thread-safe Producer/Consumer queue of delegates. Any thread that wants to update a UI component would create a delegate encapsulating the operations to be performed, and add it to the queue. The UI thread (assuming all components were created on the same thread) would then periodically pull an item from the queue, and execute the delegate.

这篇关于同一窗口上有多个UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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