<p>When you data bind in C#, the thread that changes the data causes the control to change too. But if this thread is not the one on which the control was created, you'll get an Illegal Cross Thread Operation exception.</p>

<p>Is there anyway to prevent this?</p>

[{'answer_id': 1998, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 3, 'selected': False, 'text': '<p>You should be able to do something like:</p>\n\n<pre><code>if (control.InvokeRequired)\n{\n    control.Invoke(delegateWithMyCode);\n}\nelse\n{\n    delegateWithMyCode();\n}\n</code></pre>\n\n<p>InvokeRequired is a property on Controls to see if you are on the correct thread, then Invoke will invoke the delegate on the correct thread.</p>\n\n<p>UPDATE: Actually, at my last job we did something like this:</p>\n\n<pre><code>private void SomeEventHandler(Object someParam)\n{\n    if (this.InvokeRequired)\n    {\n        this.Invoke(new SomeEventHandlerDelegate(SomeEventHandler), someParam);\n    }\n\n    // Regular handling code\n}\n</code></pre>\n\n<p>which removes the need for the else block and kind of tightens up the code.</p>\n'}, {'answer_id': 2002, 'author': 'tags2k', 'author_id': 192, 'author_profile': 'https://Stackoverflow.com/users/192', 'pm_score': 2, 'selected': False, 'text': "<p>As I don't have a test case to go from I can't guarantee this solution, but it seems to me that a scenario similar to the one used to update progress bars in different threads (use a delegate) would be suitable here.</p>\n\n<pre><code>public delegate void DataBindDelegate();\npublic DataBindDelegate BindData = new DataBindDelegate(DoDataBind);\n\npublic void DoDataBind()\n{\n    DataBind();\n}\n</code></pre>\n\n<p>If the data binding needs to be done by a particular thread, then let that thread do the work!</p>\n"}, {'answer_id': 2013, 'author': 'tags2k', 'author_id': 192, 'author_profile': 'https://Stackoverflow.com/users/192', 'pm_score': 0, 'selected': False, 'text': '<p>If the thread call is "illegal" (i.e. the DataBind call affects controls that were not created in the thread it is being called from) then you need to create a delegate so that even if the decision / preparation for the DataBind is not done in the control-creating thread, any resultant modification of them (i.e. DataBind()) will be.</p>\n\n<p>You would call my code from the worker thread like so:</p>\n\n<pre><code>this.BindData.Invoke();\n</code></pre>\n\n<p>This would then cause the original thread to do the binding, which (presuming it is the thread that created the controls) should work.</p>\n'}, {'answer_id': 6109, 'author': 'Brian Leahy', 'author_id': 580, 'author_profile': 'https://Stackoverflow.com/users/580', 'pm_score': 0, 'selected': False, 'text': '<p>In WPF and Silverlight the binding infrastructure takes care of the switching to the UI thread.</p>\n'}]
['https://Stackoverflow.com/questions/1994', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/195/']