使用InvokeRequired VS control.InvokeRequired [英] Using InvokeRequired vs control.InvokeRequired
问题描述
是什么区别 InvokeRequired
和 somecontrol.InvokeRequired
?
这样,
委托无效valueDelegate(字符串值);私人无效的SetValue(字符串值)
{
如果(InvokeRequired)
{
的BeginInvoke(新valueDelegate(的SetValue),价值);
}
其他
{
someControl.Text =价值;
}
}
和
委托无效valueDelegate(字符串值);私人无效的SetValue(字符串值)
{
如果(someControl.InvokeRequired)
{
someControl.Invoke(新valueDelegate(的SetValue),价值);
}
其他
{
someControl.Text =价值;
}
}
第一个版本检查负责的这个的控制线程。第二个版本检查负责 someControl
线程。 (和同上,用于其控制的线程他们然后委托调用到。)
他们可能是不同的 - 尽管他们真正的不应该的是,如果这两个控件都在同一个顶层窗口。 (在一个窗口中的所有控件应该在同一个线程上运行。)
What is the difference between InvokeRequired
and somecontrol.InvokeRequired
?
like this,
delegate void valueDelegate(string value);
private void SetValue(string value)
{
if (InvokeRequired)
{
BeginInvoke(new valueDelegate(SetValue),value);
}
else
{
someControl.Text = value;
}
}
and
delegate void valueDelegate(string value);
private void SetValue(string value)
{
if (someControl.InvokeRequired)
{
someControl.Invoke(new valueDelegate(SetValue),value);
}
else
{
someControl.Text = value;
}
}
The first version checks the thread responsible for this control. The second version checks the thread responsible for someControl
. (And ditto for which control's thread they then delegate the invocation to.)
They could potentially be different - although they really shouldn't be if the two controls are in the same top-level window. (All controls in one window should be running on the same thread.)
这篇关于使用InvokeRequired VS control.InvokeRequired的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!