<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx</link><description>Multi-threaded apartments do not pump messages.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8421817</link><pubDate>Thu, 24 Apr 2008 18:09:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8421817</guid><dc:creator>Tim Smith</dc:creator><description>&lt;p&gt;I'm sure that the basics of what you are saying is documented in MSDN. &amp;nbsp;However, I've always been the type of person who understands things better knowing the &amp;quot;why&amp;quot;.&lt;/p&gt;
&lt;p&gt;Of course, with pure contract programming, why isn't really important.&lt;/p&gt;
&lt;p&gt;Thanks for the info.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8421868</link><pubDate>Thu, 24 Apr 2008 18:21:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8421868</guid><dc:creator>David Walker</dc:creator><description>&lt;p&gt;I think that understanding &amp;quot;Why&amp;quot; helps you truly understand the &amp;quot;What&amp;quot;, and makes you less likely to inadvertently make mistakes. &amp;nbsp;You're also probably more likely to guess correctly in areas where the contract isn't well-defined.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8421911</link><pubDate>Thu, 24 Apr 2008 18:30:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8421911</guid><dc:creator>Yuhong Bao</dc:creator><description>&lt;p&gt;That, combined with the fact that STA threads are required to pump messages, is why non-UI threads should be MTA, while UI threads should be STA. Unfortunately Visual Studio .NET defaults to STA even for console apps.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8421973</link><pubDate>Thu, 24 Apr 2008 18:52:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8421973</guid><dc:creator>Yuhong Bao</dc:creator><description>&lt;P&gt;"Compatibility with this ancient model still exists today, thanks to the dreaded "main" threading model."&lt;/P&gt;
&lt;P&gt;VB ActiveX DLLs and controls used that threading model before a SP (3?) to VB 5 added support for the STA threading model.&lt;/P&gt;
&lt;DIV class=post&gt;[&lt;I&gt;"The less said about that threading model the better." -Raymond&lt;/I&gt;]&lt;/DIV&gt;</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8422463</link><pubDate>Thu, 24 Apr 2008 22:05:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8422463</guid><dc:creator>Yuhong Bao</dc:creator><description>&lt;p&gt;BTW, when was multi-threading support and the associated threading models added to COM? This is important if you are programming for Windows 95 or NT 3.5.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8422509</link><pubDate>Thu, 24 Apr 2008 22:21:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8422509</guid><dc:creator>jon</dc:creator><description>&lt;p&gt;Best explanation of the difference between STA and MTA I've ever read.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8422807</link><pubDate>Fri, 25 Apr 2008 00:44:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8422807</guid><dc:creator>Tim Smith</dc:creator><description>&lt;p&gt;Why is why important?&lt;/p&gt;
&lt;p&gt;It depends on how your brain is wired.&lt;/p&gt;
&lt;p&gt;For example, when I was getting my math degree, I had trouble remembering all those diverse formulas and rules. &amp;nbsp;However, by understanding the theory behind them, I could always re-derive the formulas because I understood the whys. &amp;nbsp;In many ways it provides a higher level understanding.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8423164</link><pubDate>Fri, 25 Apr 2008 04:11:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8423164</guid><dc:creator>Xepol</dc:creator><description>&lt;p&gt;This is what is known as insane by design.&lt;/p&gt;
&lt;p&gt;And now you all know why so few people even try to write multi-threaded code. &amp;nbsp;If the basic synchronization and communication tasks don't get you, strange terms and stranger design choices will.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8423266</link><pubDate>Fri, 25 Apr 2008 05:38:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8423266</guid><dc:creator>Dean Harding</dc:creator><description>&lt;p&gt;Xepol: how would you have designed multi-threaded COM in 16-bit Windows, then?&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8424138</link><pubDate>Fri, 25 Apr 2008 17:25:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8424138</guid><dc:creator>Tim Smith</dc:creator><description>&lt;p&gt;Xepol,&lt;/p&gt;
&lt;p&gt;I've been doing multithreading programming for many many years. &amp;nbsp;Yes, many new people make the mistake of trying to do the UI from non-UI threads, but that is an easy lesson to learn. &amp;nbsp;But after you learn that lesson, the complexity of doing multithreaded code is huge compared to this lesson.&lt;/p&gt;
&lt;p&gt;Complexity of multithreaded code: infinity&lt;/p&gt;
&lt;p&gt;Complexity of multithreaded UI code: infinity+1&lt;/p&gt;
&lt;p&gt;In other words, if people are having trouble doing multithreaded UI code, they are also going to have trouble doing multithreaded code in general. &amp;nbsp;The UI aspect of it doesn't add much complexity at all.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8424196</link><pubDate>Fri, 25 Apr 2008 18:01:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8424196</guid><dc:creator>noone in particular</dc:creator><description>&lt;p&gt;&amp;quot;how would you have designed multi-threaded COM in 16-bit Windows, then?&amp;quot;&lt;/p&gt;
&lt;p&gt;I would have thrown it overboard when switching to Win32 and designed something more straight.&lt;/p&gt;
&lt;p&gt;But we have heard time and time again how backward comatibility right into the palaeolithic was the one fixed requirement in the design of Windows.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8424741</link><pubDate>Fri, 25 Apr 2008 23:55:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8424741</guid><dc:creator>RandyOakley</dc:creator><description>&lt;p&gt;Multi-threaded code isn't so hard, but you need to understand the constraints and methods used for synchronising threads. &amp;nbsp;The Windows messaging structure doesn't support multiple threads and the Windows GDI gets messed up when GDI objects are allocated in one thread and deleted in a different thread. So for an app to benefit from multi-threading, the primary/UI thread runs apartment threaded and the background / worker threads run multi-threaded. &amp;nbsp;To avoid the major headaches have only the primary/UI thread pump windows messages, create windows and do GDI drawing calls. &amp;nbsp;The background threads can churn along and but not directly update the UI. &amp;nbsp;A simple technique I like to use for updating the UI after background threads have delivered data that can be used to update the UI is to use InterlockIncrement() to update a &amp;quot;this bit of the use needs to be updated&amp;quot; count. &amp;nbsp;Back on the UI thread a timer ticks -- say once every 1/30 a second and checks the &amp;quot;this bit of the ui is dirty&amp;quot; counts and repaints / updates the UI as needed and clears the dirty count. &amp;nbsp;Simple, but effective.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8426774</link><pubDate>Sat, 26 Apr 2008 12:44:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8426774</guid><dc:creator>!bofh</dc:creator><description>&lt;p&gt;But polling is bad! Last time i checked, the events API can be used perfectly across threads.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/oldnewthing/archive/2006/01/24/516808.aspx"&gt;http://blogs.msdn.com/oldnewthing/archive/2006/01/24/516808.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8430102</link><pubDate>Sun, 27 Apr 2008 07:15:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8430102</guid><dc:creator>J</dc:creator><description>&lt;p&gt;&amp;quot;right into the palaeolithic&amp;quot;&lt;/p&gt;
&lt;p&gt;When this stuff was designed, the palaeolithic was last week.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8435687</link><pubDate>Mon, 28 Apr 2008 19:47:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8435687</guid><dc:creator>RandyOakley</dc:creator><description>&lt;p&gt;It's true that events can be used. &amp;nbsp;However the are some advantanges to the simpler technquies. &amp;nbsp; 1st of all human perception limits how frequently it is useful to update the UI -- visual updates that happen more closely spaced that 1/30 will not be detetcable by the human eye. &amp;nbsp; 2nd -- &amp;quot;poling is bad&amp;quot; is a broad generalization -- the method I'm suggesting isn't actually poling in the typical sense. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using an event based model -- if five background tasks complete in one 1/30 second interval -- the UI will get repainted 5 times. &amp;nbsp; Using the &amp;quot;repaint UI periodcially when marked dirty&amp;quot; model -- in the same circumstance the UI will get painted only once -- as far a human observer is concerned -- there is no detetable difference -- but the latter approach makes more system resources availble to the background tasks.&lt;/p&gt;
</description></item><item><title>re: User interface code + multi-threaded apartment = death</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8435718</link><pubDate>Mon, 28 Apr 2008 20:05:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8435718</guid><dc:creator>Triangle</dc:creator><description>&lt;p&gt;&amp;quot;as far a human observer is concerned -- there is no detetable difference -- but the latter approach makes more system resources availble to the background tasks.&amp;quot;&lt;/p&gt;
&lt;p&gt;Don't be so sure about that. Instead of haphazardly guessing how good the users' eyes are, you should use the best measurement available: The monitor refresh rate. The most efficient UI model is one where the screen is not redrawn unless there has been a change in the contents, and the most recent redraw was within the last monitor refresh. Obviously nobody would bother to implement the whole thing on their own, however if microsoft added it to a library so third parties could use it, it could be quite efficient (And would fit very neatly into the Vista window managers' double buffering scheme)&lt;/p&gt;
</description></item><item><title>Why your COMAddIn.Object should derive from StandardOleMarshalObject</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/04/24/8420242.aspx#8849201</link><pubDate>Tue, 12 Aug 2008 00:31:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8849201</guid><dc:creator>Andrew Whitechapel</dc:creator><description>&lt;p&gt;In general, it is important that any code in a managed Office add-in should execute on the main UI thread.&lt;/p&gt;
</description></item></channel></rss>