<p>I'm sure there is a good (or at least decent) reason for this.  What is it?</p>

[{'answer_id': 10354, 'author': 'Adam Haile', 'author_id': 194, 'author_profile': 'https://Stackoverflow.com/users/194', 'pm_score': 1, 'selected': False, 'text': "<p>It is so that you don't have two things trying to update the control at the same time. (This could happen if the CPU switches to the other thread in the middle of a write/read)\nSame reason you need to use mutexes (or some other synchronization) when accessing shared variables between multiple threads.</p>\n\n<p>Edit:</p>\n\n<blockquote>\n  <p>In other languages such as C++ you are\n  free to try and do this (without an\n  exception being thrown as in\n  WinForms), but you'll end up learning\n  the hard way!</p>\n</blockquote>\n\n<p>Ahh yes...I switch between C/C++ and C# and therefore was a little more generic then I should've been, sorry... He is correct, you <em>can</em> do this in C/C++, but it will come back to bite you!</p>\n"}, {'answer_id': 10359, 'author': 'John Sibly', 'author_id': 1078, 'author_profile': 'https://Stackoverflow.com/users/1078', 'pm_score': 4, 'selected': True, 'text': '<p>Because you can easily end up with a deadlock (among other issues). </p>\n\n<p>For exmaple, your secondary thread could be trying to update the UI control, but the UI control will be waiting for a resource locked by the secondary thread to be released, so both threads end up waiting for each other to finish. As others have commented this situation is not unique to UI code, but is particularly common.</p>\n\n<p>In other languages such as C++ you are free to try and do this (without an exception being thrown as in WinForms), but your application may freeze and stop responding should a deadlock occur.  </p>\n\n<p>Incidentally, you can easily tell the UI thread that you want to update a control, just create a delegate, then call the (asynchronous) BeginInvoke method on that control passing it your delegate. E.g.</p>\n\n<pre><code>myControl.BeginInvoke(myControl.UpdateFunction);\n</code></pre>\n\n<p>This is the equivalent to doing a C++/MFC PostMessage from a worker thread</p>\n'}, {'answer_id': 10362, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 2, 'selected': False, 'text': '<p>Back in 1.0/1.1 no exception was thrown during debugging, what you got instead was an intermittent run-time hanging scenario. Nice! :)\nTherefore with 2.0 they made this scenario throw an exception and quite rightly so.</p>\n\n<p>The actual reason for this is probably (as Adam Haile states) some kind of concurrency/locky issue. \nNote that the normal .NET api (such as TextBox.Text = "Hello";) wraps SEND commands (that require immediate action) which can create issues if performed on separate thread from the one that actions the update. Using Invoke/BeginInvoke uses a POST instead which queues the action.</p>\n\n<p>More information on SEND and POST <a href="http://msdn.microsoft.com/en-us/library/ms644927.aspx#posting_sending" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 10388, 'author': 'Andrew Grant', 'author_id': 1043, 'author_profile': 'https://Stackoverflow.com/users/1043', 'pm_score': 1, 'selected': False, 'text': '<p>There would also be the need to implement synchronization within update functions that are sensitive to being called simultaneously. Doing this for UI elements would be costly at both application and OS levels, and completely redundant for the vast majority of code. </p>\n\n<p>Some APIs provide a way to change the current thread ownership of a system so you can temporarily (or permanently) update systems from other threads without needing to resort to inter-thread communication.</p>\n'}, {'answer_id': 10439, 'author': 'Brian Ensink', 'author_id': 1254, 'author_profile': 'https://Stackoverflow.com/users/1254', 'pm_score': 5, 'selected': False, 'text': '<blockquote>\n  <p>I think this is a brilliant question -\n  and I think there is need of a better\n  answer.</p>\n  \n  <p>Surely the only reason is that there\n  is something in a framework somewhere\n  that isn\'t very thread-safe. </p>\n</blockquote>\n\n<p>That "something" is almost every single instance member on every single control in System.Windows.Forms.</p>\n\n<p>The MSDN documentation for many controls in System.Windows.Forms, if not all of them, say  <em>"Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe."</em></p>\n\n<p>This means that instance members such as <code>TextBox.Text {get; set;}</code> are not <strong><em>reentrant</em></strong>.</p>\n\n<p>Making each of those instance members thread safe could introduce a lot of overhead that most applications do not need.  Instead the designers of the .Net framework decided, and I think correctly, that the burden of synchronizing access to forms controls from multiple threads should be put on the programmer.</p>\n\n<p>[Edit]</p>\n\n<p>Although this question only asks "why" here is a link to an article that explains "how":</p>\n\n<p><em>How to: Make Thread-Safe Calls to Windows Forms Controls</em> on MSDN</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms171728.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms171728.aspx</a></p>\n'}, {'answer_id': 10462, 'author': 'Björn Waide', 'author_id': 1253, 'author_profile': 'https://Stackoverflow.com/users/1253', 'pm_score': 3, 'selected': False, 'text': '<p>Although it sounds reasonable Johns answer isn\'t correct. In fact even when using Invoke you\'re still not safe not running into dead-lock situations. When dealing with events fired on a background thread using Invoke might even lead to this problem.</p>\n\n<p><br/></p>\n\n<p>The real reason has more to do with race conditions and lays back in ancient Win32 times. I can\'t explain the details here, the keywords are message pumps, WM_PAINT events and the subtle differences between "SEND" and "POST".</p>\n\n<p><br/></p>\n\n<p>Further information can be found here <a href="http://weblogs.asp.net/justin_rogers/articles/126345.aspx" rel="noreferrer">here</a> and <a href="http://msdn.microsoft.com/en-us/library/ms644927.aspx#posting_sending" rel="noreferrer">here</a>.</p>\n'}, {'answer_id': 197611, 'author': 'Chris Hughes', 'author_id': 27405, 'author_profile': 'https://Stackoverflow.com/users/27405', 'pm_score': -1, 'selected': False, 'text': "<p>Hmm I'm not pretty sure but I think that when we have a progress controls like waiting bars, progress bars we can update their values from another thread and everything works great without any glitches. </p>\n"}]
['https://Stackoverflow.com/questions/10349', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/337/']