Let’s assume that the code running in the thread that instantiated the form/control/element (usually the main thread) does not modify/access that element at the same time, is it possible to:
-
get the Text property of a TextBox.
-
enumerate a ListView.
-
subscribe to a Form’s Closing event. (Knowing that the hook will be called from the thread that instantiated that form)
I have tried all 3 and the program doesn’t seem to complain about it. I had always thought that you had to invoke any call that wants to even remotely touch anything UI related (read or write).
I understand very clearly why I need to use the IsInvokeRequired/Invoke pattern when modifying an element, but I cannot see why accessing properties/events would cause any problems.
It’s definitely possible, however, it could lead to unexpected behaviour. Also, other thread-related bugs also need to be taken into consideration e.g. race conditions/deadlocks see Managed Threading Best Practises.
I would always stick to accessing the UI on the UI thread to be on the safe side.