Bumped into this code example today:
private void Form1_Load(object sender, EventArgs e)
{
try
{
ThreadPool.QueueUserWorkItem(ShowMessage, null);
}
catch
{
}
}
private void ShowMessage(object obj)
{
try
{
label1.Text = "Test"; // does't work and just goes into catch block
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
try
{
button1.Text = "Test"; // will actually set Text property of a button and only then throw an exception, which will be caught in corresponding catch block
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
I know one should use Invoke to work with control's properties. What puzzles me is why it actually changes text field anyways despite the exception and doesn't sets label's field. I'd like to know what's happening inside it.
Here's an answer that partially explains why is it happening, so as I got it SetWindowText doesn't work for labels from different thread, but it does work for buttons. Am I right?
Also, I would like to know if this behaviour is persistent. Does it depends on something? Attached debugger? OS or .NET version maybe? 64 vs 32 bits?
Thanks in advance!