<?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>ExecutionContext vs SynchronizationContext</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx</link><description>I’ve been asked a few times recently various questions about ExecutionContext and SynchronizationContext, for example what the differences are between them, what it means to “flow” them, and how they relate to the new async/await keywords in C# and Visual</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: ExecutionContext vs SynchronizationContext</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx#10412654</link><pubDate>Fri, 19 Apr 2013 18:41:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10412654</guid><dc:creator>Sean Hanna</dc:creator><description>&lt;p&gt;It would be interesting to see some more in-depth discussion of how to properly attach data to an execution context using LogicalCallContext.LogicalSetData / LogicalGetData&lt;/p&gt;
&lt;p&gt;I recently was doing this to capture some application specific context data that needs to flow across thread boundaries. Context data like &amp;#39;username&amp;#39; from a forms / custom authentication mechanism, internal id&amp;#39;s, performance metrics, etc.. are all useful data to attach to a thread but it&amp;#39;s so low-level that doing so is intimidating. &lt;/p&gt;
&lt;p&gt;Using context like this for application code is an architectural nightmare (service locator), but trying to do parameter / constructor injection of stuff like the username when your code wasn&amp;#39;t built out to do this originally is a nightmare too. &lt;/p&gt;
&lt;p&gt;The LogicalCallContext is a Dictionary&amp;lt;object,object&amp;gt; but only performs a shallow copy on the key/value data stored inside this dictionary. &lt;/p&gt;
&lt;p&gt;There is a special hardcoded case for using ICloneable when flowing the ETW ActivityID but otherwise nothing can be flowed &amp;nbsp;using a deep copy. &lt;/p&gt;
&lt;p&gt;It seems that it is possible that Logical Call Context data is flowed &amp;#39;backwards&amp;#39; into an invoking process (or as a result of some other trickery going on) because, for example, when using LogicalCallContext in an MSTest unit test, if you leave a reference to an object of a custom type in the context when the test terminates, the object ends up in the mstest.exe process space, it attempts to resolve this type name, and if the dll that declares the type isn&amp;#39;t readily available in the GAC or AssemblyLoadPath then mstest crashes.&lt;/p&gt;
&lt;p&gt;There are also &amp;nbsp;several references on the internet citing problems with the way LogicalCallContext is flowed between thread pool threads by IIS which is an outstanding concern that i chose to ignore because async / flowing threads in IIS is not something we are doing intentionally right now, the happy-path works for us, and this application can afford that kind of assumption.&lt;/p&gt;
&lt;p&gt;Is the logical call context actually being guaranteed to flow data properly? Would an IIS threadpool not doing so be a bug? Are there other important risks to be aware of?&lt;/p&gt;
&lt;p&gt;After dealing with all these problems i ended up having to create custom implementations of IDictionary&amp;lt;&amp;gt; / ISet&amp;lt;&amp;gt; / IList&amp;lt;&amp;gt; that use LogicalCallContext as a backing data store, so that the buckets &amp;nbsp;are deep-copied when the context is flowed. Perhaps similar classes should end up in the BCL, or a more user-friendly way of performing a deep-copy should be provided...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10412654" width="1" height="1"&gt;</description></item><item><title>re: ExecutionContext vs SynchronizationContext</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx#10371405</link><pubDate>Sun, 25 Nov 2012 18:43:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10371405</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;@kyann: There&amp;#39;s no way today to modify the ExecutionContext.Capture logic. &amp;nbsp;However, you can store / retrieve data that flows with the captured context by using CallContext.LogicalSetData / CallContext.LogicalGetData.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10371405" width="1" height="1"&gt;</description></item><item><title>re: ExecutionContext vs SynchronizationContext</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx#10371395</link><pubDate>Sun, 25 Nov 2012 17:06:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10371395</guid><dc:creator>kyann</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Is it possible to &amp;quot;extend&amp;quot; the Capture to add some more info to be restored ?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10371395" width="1" height="1"&gt;</description></item><item><title>re: ExecutionContext vs SynchronizationContext</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx#10323037</link><pubDate>Fri, 22 Jun 2012 17:41:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10323037</guid><dc:creator>arnshea</dc:creator><description>&lt;p&gt;Ahh, that explains it. SynchronizationContext.Current doesn&amp;#39;t flow across await points but is captured, the way local variables passed to a lambda are captured, at the moment the continuation is registered.&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10323037" width="1" height="1"&gt;</description></item><item><title>re: ExecutionContext vs SynchronizationContext</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx#10322638</link><pubDate>Thu, 21 Jun 2012 15:41:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10322638</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;Hi Shea-&lt;/p&gt;
&lt;p&gt;The marshaling back to the UI thread is handled by the awaiter. &amp;nbsp;The example is awaiting a Task from the UI thread, so when the Task completes, the awaiter Posts back to the SynchronizationContext that was current when the await was issued.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10322638" width="1" height="1"&gt;</description></item><item><title>re: ExecutionContext vs SynchronizationContext</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx#10322055</link><pubDate>Wed, 20 Jun 2012 05:45:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10322055</guid><dc:creator>arnshea</dc:creator><description>&lt;p&gt;There&amp;#39;s an example on msdn analogous to the following:&lt;/p&gt;
&lt;p&gt;myObject_Click(...)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;await DoStuffAndUpdateUI();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;where DoStuffAndUpdateUI() in turn does an await (for a webrequest download) then *updates the UI* without explicitly marshaling back to the UI thread. However, the UI update is marshaled back onto the UI thread.&lt;/p&gt;
&lt;p&gt;Does this mean that, at least in the case of WinForms, that SynchronizationContext is flowing across awaits? e.g., it&amp;#39;s using the public ExecutionContext.Capture() ?&lt;/p&gt;
&lt;p&gt;The msdn example is at &lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/hh300224(v=vs.110)#BKMK_testAsynchSolution"&gt;msdn.microsoft.com/.../hh300224(v=vs.110)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Great post!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10322055" width="1" height="1"&gt;</description></item><item><title>re: ExecutionContext vs SynchronizationContext</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx#10321195</link><pubDate>Sun, 17 Jun 2012 21:19:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10321195</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;Nadav, as you&amp;#39;ve observed, CurrentCulture is not flowed as part of ExecutionContext.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10321195" width="1" height="1"&gt;</description></item><item><title>re: ExecutionContext vs SynchronizationContext</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx#10321070</link><pubDate>Sun, 17 Jun 2012 06:56:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10321070</guid><dc:creator>Nadav</dc:creator><description>&lt;p&gt;Does CurrentCulture get captured in the ExecutionCintext?&lt;/p&gt;
&lt;p&gt;From what I&amp;#39;ve seen in dot 4.0 it does not...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10321070" width="1" height="1"&gt;</description></item><item><title>re: ExecutionContext vs SynchronizationContext</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx#10320910</link><pubDate>Sat, 16 Jun 2012 12:15:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10320910</guid><dc:creator>tobi</dc:creator><description>&lt;p&gt;This makes me think if a [CallContextStatic] attribute would be useful in analogy to the existing [ThreadStatic] attribute.&lt;/p&gt;
&lt;p&gt;Or maybe it is better to encourage &amp;quot;empty&amp;quot; ExecutionContexts so the capturing and restoring operations are fast in the common case.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10320910" width="1" height="1"&gt;</description></item><item><title>re: ExecutionContext vs SynchronizationContext</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx#10320738</link><pubDate>Fri, 15 Jun 2012 21:19:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10320738</guid><dc:creator>svick</dc:creator><description>&lt;p&gt;Yeah, it was interesting. Thanks for writing this article.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10320738" width="1" height="1"&gt;</description></item></channel></rss>