I’m looking for a rule to know which properties of a Control prevent accessing from other threads than the thread it was created on. (say UI thread) In other words, why is it possible to access some properties of a Control from any arbitrary thread while some other ones refuse it?
:
:
{
Thread thrd1 = new Thread(DoSomething);
thrd1.Start();
}
:
:
void DoSomething()
{
// There is no problem..
dataGridView1[columnIndex1, rowIndex1].Value = "Access is free!";
dataGridView1[columnIndex1, rowIndex1].Style.BackColor = Color.Red;
// Cross-Thread operation exception would be thrown..
dataGridView1.Rows[rowIndex1].Visible = false;
}
:
:
Thanks
Basically anything whose implementation involves a window handle has affinity with the thread that created that window handle. This restriction is inherited from the underlying Windows API.
Since you can’t know the implementation, you have to assume that everything needs Invoke unless the documentation specifically calls out an exception.