I have several System.Threading.Timers on my form application with callbacks that update the UI…successfully – i.e. without throwing errors.
I had built these earlier, before I knew that UI should not be updated on any thread other than the UI thread.
Now I am confused as to why it does not throw cross-thread exceptions when I am updating UI on these separate threading.timer threads?
I will be changing these callbacks so that the UI updates are invoked on UI thread, but I am curious as to why this works.
Edit: My application is a WinForms Application.
If this is Windows Forms, it works purely by luck. Updating a UI on a background thread doesn’t consistently work, but occasionally will not throw. In particular, if CheckForIllegalCrossThreadCalls is false, it will sometimes “work”, and never throw – though the behavior is often wrong. Some controls do not always check every property, so some items “work” even though they’re not always working correctly, as this typically introduces bugs that can be difficult to diagnose and correct later.
On a side note: If this were WPF, you could change the value of a item bound to your UI, effectively updating your UI, on a background thread. The WPF binding system will automatically marshal this to the UI thread for you. It does not work to change part of a collection, however.