Control.invoke和家长控制 [英] Control.invoke and parent control

查看:127
本文介绍了Control.invoke和家长控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一个名为MainForm的形式,就可以控制:somelabel。为了从另一个线程访问这些控件我不得不使用Invoke方法。例如:

  somelabel.Invoke(...);
 

不过,我也可以通过这样的形式访问的标签

  MainForm.Invoke(...)// code操纵somelabel
 

有没有在性能或其他一些技术方面的条款这两个片段之间有什么区别?

解决方案

没有。每个Windows窗体控件公开的Invoke方法,他们都工作方式相同。无论哪管你使用(无论是形式还是子控件),你还是会做同样的基本操作,所以性能将是相同的。

我个人而言,preFER使用的形式,而不是单独的控制。这使得它更容易以后返工的设计,你可以删除或不破坏你的code调用通过表单添加控件。另一个很好的选择是使用<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.windowsformssynchronizationcontext.aspx"相对=nofollow> WindowsFormsSynchronizationContext ,您可以通过的 SynchronizationContext.Current 。这给你一个方法来生成一个同步环境,你可以使用(通过发送/发表的,而不是调用),做同样的事情,但不依赖于任何控制。

Lets say I have a form called MainForm and a control on it: somelabel. In order to access these controls from another thread I have to use Invoke method. e.g:

somelabel.Invoke(...);

However I can also access the label through the form like this:

MainForm.Invoke(...) //Code for manipulating somelabel

Is there any difference between these two snippets in terms of performance or some other technical aspect?

解决方案

No. Every Windows forms control exposes an Invoke method, and they all function the same way. No matter which control you use (whether the form or a child control), you'll still doing the same basic operation, so performance will be the same.

I, personally, prefer to use the form instead of individual controls. This makes it easier to rework the design later, as you can remove or add controls without breaking your code that invokes through the form. Another good option is to use the WindowsFormsSynchronizationContext, which you can retrieve via SynchronizationContext.Current. This gives you a way to generate a synchronization context you can use (via Send/Post instead of Invoke) that does the same thing, but is not tied to any control.

这篇关于Control.invoke和家长控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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