I see there are a lot of questions regarding threads modifying items in a form;
however, I just wanna read the state of a checkbox (for example) from a thread. So…
Is there any additional overhead to access the “Checked” state of a checkbox in a form (from a thread) -vs- creation of an bool that will reflect the state of that checkbox, and have the thread access the boolean instead?
The answer is most definitely yes. The reason is because accessing UI control state correctly involves a lot of work. You basically have to marshal a method call onto the UI thread to access a control properly. That marshaling operation requires sending a message to the UI thread via the message pump that contains a delegate reference. The UI thread will eventually pick it up and execute it. Since I have never timed this operation I cannnot say for sure how expensive it is, but I suspect it is a nontrivial amount of time. It has to be done this way because UI controls have thread affinity. That means they can only be accessed on the thread that created them. To marshal a delegate use the
Control.InvokeorControl.BeginInvokemethods.Fredrik already mentioned one really good workaround for avoiding the marshaling operation. Here is what it might look like in code.