<?xml version="1.0" encoding="utf-8"?><!-- generator="wordpress/1.5.1.3" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Updating a list from a thread - the UI control should do the checking</title>
	<link>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/</link>
	<description>General musings and programming stuff</description>
	<pubDate>Thu, 20 Nov 2008 16:51:59 +0000</pubDate>
	<generator>http://wordpress.org/?v=1.5.1.3</generator>

	<item>
		<title>by: Drew Noakes</title>
		<link>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-89297</link>
		<pubDate>Fri, 20 Apr 2007 22:06:37 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-89297</guid>
					<description>The solution at http://www.windowsforms.net/articles/asyncbindinglist.aspx involves a dependency upon System.Windows.Forms that I'd rather not have in my data assembly.  I'd propose an alternative solution that wraps an existing IBindingList (takes it in the constructor) and marshalls the events across the thread boundary in the wrapper.

In the end I just went with a 3rd party control though :)</description>
		<content:encoded><![CDATA[	<p>The solution at <a href='http://www.windowsforms.net/articles/asyncbindinglist.aspx'>http://www.windowsforms.net/articles/asyncbindinglist.aspx</a> involves a dependency upon System.Windows.Forms that I&#8217;d rather not have in my data assembly.  I&#8217;d propose an alternative solution that wraps an existing IBindingList (takes it in the constructor) and marshalls the events across the thread boundary in the wrapper.</p>
	<p>In the end I just went with a 3rd party control though <img src='http://www.sturmnet.org/blog/wp-images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Eric</title>
		<link>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-65499</link>
		<pubDate>Thu, 28 Dec 2006 17:19:24 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-65499</guid>
					<description>Oliver, I completely agree with you 100%. What a stupid decision on Microsoft's part not to handle this issue in the OnUpdateMethod of the DataGridView.

I too am looking for a proper solution to this problem, and thus far the best I have found is similar to Kenneth's suggestion to use Timers. However, you are going to want to write code on your form which listens to the ListUpdate event and enable the timer only then, disabling it in the timer_OnTick method so that you are not letting the timer call the UI thread for no reason. 

In addition, you'll want to keep track of which rows have changed, and only force the UI to update those rows or if add/deletes have occured, refresh the entire grid.

In actuality, this creates a more streamlined GUI than if you have to Invoke over to the GUI every time a single cell changes.

Anyway, please let me know if you have come to any further conclusions about this issue.

-Eric</description>
		<content:encoded><![CDATA[	<p>Oliver, I completely agree with you 100%. What a stupid decision on Microsoft&#8217;s part not to handle this issue in the OnUpdateMethod of the DataGridView.</p>
	<p>I too am looking for a proper solution to this problem, and thus far the best I have found is similar to Kenneth&#8217;s suggestion to use Timers. However, you are going to want to write code on your form which listens to the ListUpdate event and enable the timer only then, disabling it in the timer_OnTick method so that you are not letting the timer call the UI thread for no reason. </p>
	<p>In addition, you&#8217;ll want to keep track of which rows have changed, and only force the UI to update those rows or if add/deletes have occured, refresh the entire grid.</p>
	<p>In actuality, this creates a more streamlined GUI than if you have to Invoke over to the GUI every time a single cell changes.</p>
	<p>Anyway, please let me know if you have come to any further conclusions about this issue.</p>
	<p>-Eric
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Andrew Gruskin</title>
		<link>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-49295</link>
		<pubDate>Sun, 20 Aug 2006 09:54:53 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-49295</guid>
					<description>I am trying an idea loosely based around &amp;#34;The UI control should do the checking&amp;#34;.  Instead it's more of a &amp;#34;The UI control should register it existence to the IBindingList&amp;#34;.

First, I've done passing a reference of the UI control to the IBindingList instance but that is bad as it does couple my UI too much to the data layer.

Instead the UI registers itself to a helper class.  The IBindingList class checks the helper to see if a UI control exists and if so calls update on the UI thread.

This solution is neater, to me, as my data layer &amp;#34;engine&amp;#34; may be in a UI application.  If so, then the IBindingList doesn't find a registered UI control (and thread) to update on.

(And I am using XtraGrid, but I'm sure that's unimportant).</description>
		<content:encoded><![CDATA[	<p>I am trying an idea loosely based around &quot;The UI control should do the checking&quot;.  Instead it&#8217;s more of a &quot;The UI control should register it existence to the IBindingList&quot;.</p>
	<p>First, I&#8217;ve done passing a reference of the UI control to the IBindingList instance but that is bad as it does couple my UI too much to the data layer.</p>
	<p>Instead the UI registers itself to a helper class.  The IBindingList class checks the helper to see if a UI control exists and if so calls update on the UI thread.</p>
	<p>This solution is neater, to me, as my data layer &quot;engine&quot; may be in a UI application.  If so, then the IBindingList doesn&#8217;t find a registered UI control (and thread) to update on.</p>
	<p>(And I am using XtraGrid, but I&#8217;m sure that&#8217;s unimportant).
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Kenneth E.</title>
		<link>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-43199</link>
		<pubDate>Sat, 29 Jul 2006 14:37:39 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-43199</guid>
					<description>Well, I think this problem is not exactly introduced by .net, as it's a lot older than .net itself :-)

The problem has always tricked developers to do all kinds of weird workarounds in order to avoid synchronization issues between threads and especially the UI. 

If we look aside from all those kind of &amp;#34;spooky&amp;#34; workarounds I've made through time, I finally found a way that *always* works, and at the same time is extremely simple...

Use Timer(s) on the form to do all updates and get (synchonize) all &amp;#34;external&amp;#34; data from other processes and threads. The timer will always execute in the GUI thread, and there's no circular events which in turns feed themself and so on. Always clear, clean and simple.

You can probably do more exact timing in your GUI by calling directly into (or triggering) it from external threads and so on, but you pay a high price in increased complexity (and sometimes performance as well). So let the UI handle all UI tasks and let it also decide when and how often to update itself. Ofcourse you should ensure all means of thread safety required when reading from the &amp;#34;external&amp;#34; data sources.

I've done a fair amoung of thread development over time, and as simple this &amp;#34;design&amp;#34; may look, as much I love it for saving my time and solving all those weird, and sometimes almost impossible to find bugs that comes along with a complex design involving synchronization between 30+ threads.

In other words, don't trigger or call into the UI from your external threads. This way you always knows who is in control at any time, as many synchronization/thread issues is actually about who has responsability at which point in time.</description>
		<content:encoded><![CDATA[	<p>Well, I think this problem is not exactly introduced by .net, as it&#8217;s a lot older than .net itself <img src='http://www.sturmnet.org/blog/wp-images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
	<p>The problem has always tricked developers to do all kinds of weird workarounds in order to avoid synchronization issues between threads and especially the UI. </p>
	<p>If we look aside from all those kind of &quot;spooky&quot; workarounds I&#8217;ve made through time, I finally found a way that *always* works, and at the same time is extremely simple&#8230;</p>
	<p>Use Timer(s) on the form to do all updates and get (synchonize) all &quot;external&quot; data from other processes and threads. The timer will always execute in the GUI thread, and there&#8217;s no circular events which in turns feed themself and so on. Always clear, clean and simple.</p>
	<p>You can probably do more exact timing in your GUI by calling directly into (or triggering) it from external threads and so on, but you pay a high price in increased complexity (and sometimes performance as well). So let the UI handle all UI tasks and let it also decide when and how often to update itself. Ofcourse you should ensure all means of thread safety required when reading from the &quot;external&quot; data sources.</p>
	<p>I&#8217;ve done a fair amoung of thread development over time, and as simple this &quot;design&quot; may look, as much I love it for saving my time and solving all those weird, and sometimes almost impossible to find bugs that comes along with a complex design involving synchronization between 30+ threads.</p>
	<p>In other words, don&#8217;t trigger or call into the UI from your external threads. This way you always knows who is in control at any time, as many synchronization/thread issues is actually about who has responsability at which point in time.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Oliver Sturm</title>
		<link>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-42079</link>
		<pubDate>Fri, 21 Jul 2006 18:54:00 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-42079</guid>
					<description>That's an interesting post you are pointing to, but it tries to find a way to work with the broken architecture in standard Windows Forms (in this regard, of course) instead of proposing a solution to fix the breakage. 

The approach seems to introduce an enormous overhead and it has the requirement of instantiating the collections in the UI thread, which I think can be pretty difficult to comply with in real-world applications (or libraries/frameworks, even worse). I'm sure it would be a great deal easier to simply let the UI control itself do the work - after all, the requirement to run on the UI thread is a requirement introduced by the UI control in the first place, so it should have the responsibility of assuring that this is adhered to. Not to mention it's simply the easiest way.

And finally, I can't imagine Microsoft made their decision to NOT do this in their UI controls because they knew this Daniel guy would come up with his idea :-) I'm not much closer to understanding why MS (specifically, but probably loads of other component writers) decided to go the way they did.

Hey, thanks for your comment - I certainly appreciate the pointer!</description>
		<content:encoded><![CDATA[	<p>That&#8217;s an interesting post you are pointing to, but it tries to find a way to work with the broken architecture in standard Windows Forms (in this regard, of course) instead of proposing a solution to fix the breakage. </p>
	<p>The approach seems to introduce an enormous overhead and it has the requirement of instantiating the collections in the UI thread, which I think can be pretty difficult to comply with in real-world applications (or libraries/frameworks, even worse). I&#8217;m sure it would be a great deal easier to simply let the UI control itself do the work - after all, the requirement to run on the UI thread is a requirement introduced by the UI control in the first place, so it should have the responsibility of assuring that this is adhered to. Not to mention it&#8217;s simply the easiest way.</p>
	<p>And finally, I can&#8217;t imagine Microsoft made their decision to NOT do this in their UI controls because they knew this Daniel guy would come up with his idea <img src='http://www.sturmnet.org/blog/wp-images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  I&#8217;m not much closer to understanding why MS (specifically, but probably loads of other component writers) decided to go the way they did.</p>
	<p>Hey, thanks for your comment - I certainly appreciate the pointer!
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Oliver Sturm</title>
		<link>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-42078</link>
		<pubDate>Fri, 21 Jul 2006 18:39:19 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-42078</guid>
					<description>This problem doesn't have anything to do with the binding list being thread safe or not, as the list is only updated from one thread. You are right that my sample doesn't end the thread in any way - that's because it's a sample :-) But the exception in question is triggered not on program exit, but rather after the very first update to the collection.

Anyway, this is all not the problem - I understand every tiny little bit about the original problem (and the solution) of synching into the UI thread. The thing I'm talking about is that if things are left to me as the programmer of a collection class (instead of the UI control programmer handling things), there's really no way for me to do the job correctly, because the UI thread is simply something I'm not concerned with.</description>
		<content:encoded><![CDATA[	<p>This problem doesn&#8217;t have anything to do with the binding list being thread safe or not, as the list is only updated from one thread. You are right that my sample doesn&#8217;t end the thread in any way - that&#8217;s because it&#8217;s a sample <img src='http://www.sturmnet.org/blog/wp-images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  But the exception in question is triggered not on program exit, but rather after the very first update to the collection.</p>
	<p>Anyway, this is all not the problem - I understand every tiny little bit about the original problem (and the solution) of synching into the UI thread. The thing I&#8217;m talking about is that if things are left to me as the programmer of a collection class (instead of the UI control programmer handling things), there&#8217;s really no way for me to do the job correctly, because the UI thread is simply something I&#8217;m not concerned with.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: John Gooding</title>
		<link>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-42075</link>
		<pubDate>Fri, 21 Jul 2006 17:46:33 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2006/07/21/updating-a-list-from-a-thread-the-ui-control-should-do-the-checking/#comment-42075</guid>
					<description>I think the real issue is that BindingList isn't thread safe :)

I found an article that has similar code to what I use  http://www.windowsforms.net/articles/asyncbindinglist.aspx in your sample it will throw a null reference or thread error on exit only because there is no thread join / clean exit for the background thread.
Hope that helps.

John</description>
		<content:encoded><![CDATA[	<p>I think the real issue is that BindingList isn&#8217;t thread safe <img src='http://www.sturmnet.org/blog/wp-images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
	<p>I found an article that has similar code to what I use  <a href='http://www.windowsforms.net/articles/asyncbindinglist.aspx'>http://www.windowsforms.net/articles/asyncbindinglist.aspx</a> in your sample it will throw a null reference or thread error on exit only because there is no thread join / clean exit for the background thread.<br />
Hope that helps.</p>
	<p>John
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
