<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Synchronizing .NET 4 Tasks with the UI Thread</title>
	<atom:link href="http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/feed/" rel="self" type="application/rss+xml" />
	<link>http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/</link>
	<description>Thoughts on C#, WPF, .NET, and programming for Scientific Visualization</description>
	<lastBuildDate>Fri, 11 May 2012 16:07:35 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Reed</title>
		<link>http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/#comment-2144</link>
		<dc:creator>Reed</dc:creator>
		<pubDate>Wed, 18 Apr 2012 08:52:12 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=88#comment-2144</guid>
		<description>This shouldn&#039;t happen, normally - though it depends on the current TaskScheduler, as Task.Factory will use the current TaskScheduler to schedule the task.</description>
		<content:encoded><![CDATA[<p>This shouldn&#8217;t happen, normally &#8211; though it depends on the current TaskScheduler, as Task.Factory will use the current TaskScheduler to schedule the task.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew</title>
		<link>http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/#comment-2143</link>
		<dc:creator>Andrew</dc:creator>
		<pubDate>Tue, 17 Apr 2012 22:23:33 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=88#comment-2143</guid>
		<description>@Reed - I have also found several situations where Task.Factory.StartNew ends up running on the UI Thread.</description>
		<content:encoded><![CDATA[<p>@Reed &#8211; I have also found several situations where Task.Factory.StartNew ends up running on the UI Thread.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Glen Harvy</title>
		<link>http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/#comment-2106</link>
		<dc:creator>Glen Harvy</dc:creator>
		<pubDate>Wed, 14 Mar 2012 17:46:39 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=88#comment-2106</guid>
		<description>Thanks again Reed, works like a charm :-)</description>
		<content:encoded><![CDATA[<p>Thanks again Reed, works like a charm <img src='http://reedcopsey.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reed</title>
		<link>http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/#comment-2102</link>
		<dc:creator>Reed</dc:creator>
		<pubDate>Mon, 12 Mar 2012 23:09:31 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=88#comment-2102</guid>
		<description>Glen,

One easy approach is actually to take your idea one step futher - instead of making a TaskScheduler and passing it around, you can create a TaskFactory and pass it around:

    TaskFactory uiFactory = new TaskFactory(TaskScheduler.FromCurrentSynchronizationContext());

Then, in your background thread, updating a UI item becomes:

    string newText = ComputeText(); // Running in background thread/task
    uiFactory.StartNew(() =&gt; this.textBox1.Text = newText);

It&#039;s the same idea, but a bit less typing...

-Reed</description>
		<content:encoded><![CDATA[<p>Glen,</p>
<p>One easy approach is actually to take your idea one step futher &#8211; instead of making a TaskScheduler and passing it around, you can create a TaskFactory and pass it around:</p>
<p>    TaskFactory uiFactory = new TaskFactory(TaskScheduler.FromCurrentSynchronizationContext());</p>
<p>Then, in your background thread, updating a UI item becomes:</p>
<p>    string newText = ComputeText(); // Running in background thread/task<br />
    uiFactory.StartNew(() => this.textBox1.Text = newText);</p>
<p>It&#8217;s the same idea, but a bit less typing&#8230;</p>
<p>-Reed</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Glen Harvy</title>
		<link>http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/#comment-2101</link>
		<dc:creator>Glen Harvy</dc:creator>
		<pubDate>Mon, 12 Mar 2012 02:52:19 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=88#comment-2101</guid>
		<description>Hi,

Thanks for this posting. I needed an easy way to update the user thread in a GUI application when the task finished and this works like a treat. What I did need to do however is change the declaration of the context to

      TaskScheduler context = TaskScheduler.FromCurrentSynchronizationContext();

and then pass the context to the task itself as a parameter.

If there is a simple way to update a control on the GUI thread without stopping the GUI thread then I would appreciate an uncomplicated example (like in updating a marqueeControl). I must be missing something because with the &#039;old&#039; background threading scenario you just created an event.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Thanks for this posting. I needed an easy way to update the user thread in a GUI application when the task finished and this works like a treat. What I did need to do however is change the declaration of the context to</p>
<p>      TaskScheduler context = TaskScheduler.FromCurrentSynchronizationContext();</p>
<p>and then pass the context to the task itself as a parameter.</p>
<p>If there is a simple way to update a control on the GUI thread without stopping the GUI thread then I would appreciate an uncomplicated example (like in updating a marqueeControl). I must be missing something because with the &#8216;old&#8217; background threading scenario you just created an event.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reed</title>
		<link>http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/#comment-1858</link>
		<dc:creator>Reed</dc:creator>
		<pubDate>Wed, 02 Nov 2011 18:49:59 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=88#comment-1858</guid>
		<description>Paul,

This is required only because there is no overload for TaskFactory.StartNew that takes a TaskScheduler but not the token and creation options.  The only other option is to create the task, then use Task.Start(scheduler).  Otherwise, you need to specify the other items, as the Task API just exposes it that way.

-Reed</description>
		<content:encoded><![CDATA[<p>Paul,</p>
<p>This is required only because there is no overload for TaskFactory.StartNew that takes a TaskScheduler but not the token and creation options.  The only other option is to create the task, then use Task.Start(scheduler).  Otherwise, you need to specify the other items, as the Task API just exposes it that way.</p>
<p>-Reed</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul</title>
		<link>http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/#comment-1857</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Wed, 02 Nov 2011 09:20:49 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=88#comment-1857</guid>
		<description>Hi Reed.

when starting a Task directly on the UI thread why the need for the cancellation token and TaskCreationOptions?

thanks
Paul</description>
		<content:encoded><![CDATA[<p>Hi Reed.</p>
<p>when starting a Task directly on the UI thread why the need for the cancellation token and TaskCreationOptions?</p>
<p>thanks<br />
Paul</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver</title>
		<link>http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/#comment-1147</link>
		<dc:creator>Oliver</dc:creator>
		<pubDate>Sat, 09 Oct 2010 11:24:45 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=88#comment-1147</guid>
		<description>Thanks for the post, helpful!

A much easier way of approaching threading.</description>
		<content:encoded><![CDATA[<p>Thanks for the post, helpful!</p>
<p>A much easier way of approaching threading.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reed</title>
		<link>http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/#comment-1121</link>
		<dc:creator>Reed</dc:creator>
		<pubDate>Tue, 06 Jul 2010 17:58:18 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=88#comment-1121</guid>
		<description>Tillias,

That&#039;s actually not accurate.  The default TaskScheduler, which is what is used when you do Task.Factory.StartNew, will use always use a ThreadPool thread to execute your Task, unless the LongRunning hint is specified, in which case it will use a dedicated thread (in .NET 4&#039;s implementation).  Either way, it should NEVER run on the UI thread, since the UI thread should never be on a ThreadPool thread.  

Tasks will only get scheduled on the current thread when the current thread is a threadpool thread, or the current thread is blocking on the result of a collection of Tasks (including the one that needs to execute).

That being said, if you want to force a task to run on a dedicated thread, you can do so by specifying the LongRunning hint (in the current implementation).  This is a much simpler alternative to creating a custom TaskScheduler.  Using an STA thread scheduler is really overkill, unless you specifically need an STA thread.

-Reed</description>
		<content:encoded><![CDATA[<p>Tillias,</p>
<p>That&#8217;s actually not accurate.  The default TaskScheduler, which is what is used when you do Task.Factory.StartNew, will use always use a ThreadPool thread to execute your Task, unless the LongRunning hint is specified, in which case it will use a dedicated thread (in .NET 4&#8242;s implementation).  Either way, it should NEVER run on the UI thread, since the UI thread should never be on a ThreadPool thread.  </p>
<p>Tasks will only get scheduled on the current thread when the current thread is a threadpool thread, or the current thread is blocking on the result of a collection of Tasks (including the one that needs to execute).</p>
<p>That being said, if you want to force a task to run on a dedicated thread, you can do so by specifying the LongRunning hint (in the current implementation).  This is a much simpler alternative to creating a custom TaskScheduler.  Using an STA thread scheduler is really overkill, unless you specifically need an STA thread.</p>
<p>-Reed</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tillias</title>
		<link>http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/#comment-1120</link>
		<dc:creator>tillias</dc:creator>
		<pubDate>Tue, 06 Jul 2010 17:10:53 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=88#comment-1120</guid>
		<description>Hi Reed! 
Task doesn&#039;t guarantee that it will be executed in separate thread. I&#039;ve faced several common scenarios where Task&#039;s delegate is executed in GUI thread and thus the application is stuck. Custom TaskScheduler must be used in order to guarantee that task will be executed in separate thread ( and not in the same GUI one).

You can reed about it here: http://tillias.wordpress.com/2010/07/05/net-4-system-threading-tasks-and-wpf-ui-thread/</description>
		<content:encoded><![CDATA[<p>Hi Reed!<br />
Task doesn&#8217;t guarantee that it will be executed in separate thread. I&#8217;ve faced several common scenarios where Task&#8217;s delegate is executed in GUI thread and thus the application is stuck. Custom TaskScheduler must be used in order to guarantee that task will be executed in separate thread ( and not in the same GUI one).</p>
<p>You can reed about it here: <a href="http://tillias.wordpress.com/2010/07/05/net-4-system-threading-tasks-and-wpf-ui-thread/" rel="nofollow">http://tillias.wordpress.com/2010/07/05/net-4-system-threading-tasks-and-wpf-ui-thread/</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
 
