I have the following Windows Forms code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
new Thread(SampleFunction).Start();
}
void SampleFunction()
{
for (int i = 0; i < 10; i++)
{
if (textBox1.InvokeRequired == true)
textBox1.Invoke((MethodInvoker)delegate { textBox1.Text += "HI. "; });
else
textBox1.Text += "HII. "; // Sometimes hit on first pass of the loop.
Thread.Sleep(1000);
}
}
When debugging the above code using breakpoints, I am observing that the non-invoke-required path is hit on a first pass, but only once about every 10 runs. I am surprised because the code is on a separate thread, and I am expecting InvokeRequired to be true at all times. Can someone please shed light?
It most likely happens because you start the thread in the form's constructor. At that point the form isn't shown yet and its window handle might not have been created by the time the first iteration is performed.
From the MSDN:
So the way I see it you have two options. 1) Wait until the handle has been created by checking the
IsHandleCreatedproperty:2) Check the
Handleproperty to force a window handle to be created (I put it in awhileloop just in case):As you see I access
thisrather thantextBox1. I recommend you to do this when checking and invoking as well becauseControl.Invoke()will lookup the absolute parent (the form) anyway.