Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
172 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...