线程中的 UI 容器?
代码中的兄弟。
我正在尝试使我的 WinForms 应用程序成为多线程。在我的后台工作人员的 DoWork 中,我有一个使用 MethodInvoker 委托更改一些控件的方法。我的问题是,每次我想从另一个线程更改每个控件时,是否都必须调用它,或者是否可以调用某种控件容器来避免多次调用某些控件?
brothers in code.
I'm trying to make my WinForms app multi threaded. In DoWork of my background worker I've got a method which changes few controls using MethodInvoker delegate. My question is if I have to invoke every control every time I want to change it from another thread or maybe there is some kind of container of controls which I can invoke to avoid multiple invoking certain controls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设您想要更改两个
Label
上的文本。假设它们属于同一个Form
,您可以通过单独调用Invoke
...或通过分组到单个
Invoke< /code>,以节省一些打字并提高性能。
Let's say you want to change the text on two
Label
s. Assuming they belong to the sameForm
, you can do this either by individual calls toInvoke
......or by grouping then in a single
Invoke
, to save some typing and increase performance.从技术上讲,您的所有 GUI 都是在主线程上创建的,因此,如果您调用 GUI 上的主面板,那么在该调用方法中,您可以在该方法中更改 GUI 上的其他控件,
另外,如果您的后台工作程序是在主线程上创建的然后您可以在主线程上调用报告进度事件...这意味着不需要调用。后台工作者的主要目的确实如此。
Technically all of your GUI is created on the main thread, so If you invoke say a main panel on the GUI then within that invocation method you can alter other controls on the GUI all within that method
Plus if your background worker was created on main thread then you can call report progress event back on main thread... Which means invocation not required. Main purpose of background workers really.
调用意味着安排您的代码在拥有控件的线程上运行,在所有简单的情况下,对于您的所有控件来说,该线程都将是同一个线程。因此,虽然每次想要与控件交互时都必须调用,但实际上您可以将任意数量的交互“池化”在一起,并且仅对整个部分调用一次(这样做会提高性能)。
如果你想“隐藏”调用,你必须编写一个类,当触发时,它会检测其属性的更改,并在与控件交互的代码上使用
Invoke
,其方式取决于这些属性。因此工作流程是:Invoking means scheduling your code to run on the thread that owns the controls, which in all straightforward cases would be the very same thread for all of your controls. So while you do have to invoke every time you want to interact with a control, you can in practice "pool" as many interactions as you wish together and only invoke once for the whole piece (doing so will be more performant).
If you want to "hide" the invocations you 'd have to write a class that, when triggered, would detect changes to its properties and use
Invoke
on code that interacts with your controls in a manner dependent on these properties. So the workflow would be:每次想要更改 UI 时都必须调用。调用操作不仅可以更改一个属性,还可以是更新 100 个控件的完整功能。
最小化调用有利于性能。
不,没有预定义的容器。假设您自己能够编程调用,例如匿名代码块。
You have to invoke every time yiou want to change the UI. The invoke operation can do more than changing one peroeprty -it can be a complete function udpating 100 controls.
Minimizing invokes is good for performance.
No, there is no predefined container. You are assumed to be an able program an invoke, for example for an anonymous code block, yourself.