<?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: Asynchronous programming for XNA games with Squared.Task</title>
	<atom:link href="http://www.luminance.org/code/2009/06/13/asynchronous-programming-for-xna-games-with-squaredtask/feed" rel="self" type="application/rss+xml" />
	<link>http://www.luminance.org/blog/code/2009/06/13/asynchronous-programming-for-xna-games-with-squaredtask</link>
	<description>Programming and Game Development - Kevin Gadd&#039;s Blog</description>
	<lastBuildDate>Wed, 18 Jan 2012 18:18:28 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Kael</title>
		<link>http://www.luminance.org/blog/code/2009/06/13/asynchronous-programming-for-xna-games-with-squaredtask/comment-page-1#comment-774</link>
		<dc:creator>Kael</dc:creator>
		<pubDate>Tue, 16 Feb 2010 01:27:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.luminance.org/?p=465#comment-774</guid>
		<description>&lt;blockquote cite=&quot;#comment-body-773&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;#comment-773&quot; rel=&quot;nofollow&quot;&gt;Robert&lt;/a&gt; :&lt;/strong&gt;
                  &lt;div class=&quot;avatar&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://www.gravatar.com/avatar/49e52e2e71d8e14bc6259c1236496d0e?s=64&amp;d=identicon&amp;r=PG&quot; class=&quot;avatar avatar-64 photo&quot; height=&quot;64&quot; width=&quot;64&quot;/&gt;&lt;/div&gt;
         
         
         &lt;p&gt;How can I use the TaskScheduler in a Windows Service application? In the sample console MUDServer, I see that you are using an infinite loop which does not allow the Main method to exit:&lt;br /&gt;
            while (true) {&lt;br /&gt;
                Scheduler.Step();&lt;br /&gt;
                Scheduler.WaitForWorkItems();&lt;br /&gt;
            }&lt;/p&gt;
&lt;p&gt;How can I start the task scheduler in my service’s OnStart method? OnStart must finish so that Windows considers the service as successfully started.&lt;/p&gt;
&lt;p&gt;There is also the complementary action: stopping… How can I end the taskscheduler in OnStop?&lt;/p&gt;
         &lt;a id=&quot;comment-reply-773&quot; rel=&quot;nofollow&quot;&gt;&lt;/a&gt;
       &lt;/blockquote&gt;
Hi Robert,

I haven&#039;t personally tried writing a Service using the TaskScheduler. My suggestion would be to try using the windows message pump to provide the Step/Wait functionality, because then the scheduler stops automatically with your message pump. I use this approach in graphical applications and servers and in both cases it works well because you can use the standard System.Windows.Forms.Application methods/events to handle things like system shutdown. Having a message pump within a Service is well understood in Win32 and I&#039;m pretty sure there are examples for how to do it in C#.

FYI, to create a scheduler based on the windows message pump, you just do:
&lt;tt&gt;new TaskScheduler(JobQueue.WindowsMessageBased)&lt;/tt&gt;
Any tasks queued to the scheduler will run automatically by the windows message pump (they get mapped to windows messages).

A few gotchas:
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Scheduling using the windows message pump is considerably slower than the normal way (you peak at around 30000-50000 task &quot;steps&quot; per second, compared to millions/sec with the normal approach), so if you&#039;re doing complex computations using communication between tasks, you&#039;ll get bottlenecked. If you need to do complex computation via tasks, you can always run a normal scheduler in a worker thread.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;WaitForNextStep will no longer work since windows&#039; message pump has no concept of &#039;steps&#039;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since you&#039;re running on the UI thread inside the message pump, if your individual tasks block (by waiting on a WaitHandle, or doing a bunch of computation, etc) that will block your UI thread and Windows will act like your app has hung.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
		<content:encoded><![CDATA[<blockquote cite="#comment-body-773"><p>
<strong><a href="#comment-773" rel="nofollow">Robert</a> :</strong></p>
<div class="avatar"><img alt="" src="http://www.gravatar.com/avatar/49e52e2e71d8e14bc6259c1236496d0e?s=64&amp;d=identicon&amp;r=PG" class="avatar avatar-64 photo" height="64" width="64"/></div>
<p>How can I use the TaskScheduler in a Windows Service application? In the sample console MUDServer, I see that you are using an infinite loop which does not allow the Main method to exit:<br />
            while (true) {<br />
                Scheduler.Step();<br />
                Scheduler.WaitForWorkItems();<br />
            }</p>
<p>How can I start the task scheduler in my service’s OnStart method? OnStart must finish so that Windows considers the service as successfully started.</p>
<p>There is also the complementary action: stopping… How can I end the taskscheduler in OnStop?</p>
<p>         <a id="comment-reply-773" rel="nofollow"></a>
       </p></blockquote>
<p>Hi Robert,</p>
<p>I haven&#8217;t personally tried writing a Service using the TaskScheduler. My suggestion would be to try using the windows message pump to provide the Step/Wait functionality, because then the scheduler stops automatically with your message pump. I use this approach in graphical applications and servers and in both cases it works well because you can use the standard System.Windows.Forms.Application methods/events to handle things like system shutdown. Having a message pump within a Service is well understood in Win32 and I&#8217;m pretty sure there are examples for how to do it in C#.</p>
<p>FYI, to create a scheduler based on the windows message pump, you just do:<br />
<tt>new TaskScheduler(JobQueue.WindowsMessageBased)</tt><br />
Any tasks queued to the scheduler will run automatically by the windows message pump (they get mapped to windows messages).</p>
<p>A few gotchas:</p>
<ul>
<li>
<p>Scheduling using the windows message pump is considerably slower than the normal way (you peak at around 30000-50000 task &#8220;steps&#8221; per second, compared to millions/sec with the normal approach), so if you&#8217;re doing complex computations using communication between tasks, you&#8217;ll get bottlenecked. If you need to do complex computation via tasks, you can always run a normal scheduler in a worker thread.</p>
</li>
<li>
<p>WaitForNextStep will no longer work since windows&#8217; message pump has no concept of &#8216;steps&#8217;.</p>
</li>
<li>
<p>Since you&#8217;re running on the UI thread inside the message pump, if your individual tasks block (by waiting on a WaitHandle, or doing a bunch of computation, etc) that will block your UI thread and Windows will act like your app has hung.</p>
</li>
</ul>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert</title>
		<link>http://www.luminance.org/blog/code/2009/06/13/asynchronous-programming-for-xna-games-with-squaredtask/comment-page-1#comment-773</link>
		<dc:creator>Robert</dc:creator>
		<pubDate>Mon, 15 Feb 2010 22:54:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.luminance.org/?p=465#comment-773</guid>
		<description>How can I use the TaskScheduler in a Windows Service application? In the sample console MUDServer, I see that you are using an infinite loop which does not allow the Main method to exit:
            while (true) {
                Scheduler.Step();
                Scheduler.WaitForWorkItems();
            }

How can I start the task scheduler in my service&#039;s OnStart method? OnStart must finish so that Windows considers the service as successfully started.

There is also the complementary action: stopping... How can I end the taskscheduler in OnStop?</description>
		<content:encoded><![CDATA[<p>How can I use the TaskScheduler in a Windows Service application? In the sample console MUDServer, I see that you are using an infinite loop which does not allow the Main method to exit:<br />
            while (true) {<br />
                Scheduler.Step();<br />
                Scheduler.WaitForWorkItems();<br />
            }</p>
<p>How can I start the task scheduler in my service&#8217;s OnStart method? OnStart must finish so that Windows considers the service as successfully started.</p>
<p>There is also the complementary action: stopping&#8230; How can I end the taskscheduler in OnStop?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Concurrency on a single thread &#8211; 22th Edition &#124; ArcadePortal</title>
		<link>http://www.luminance.org/blog/code/2009/06/13/asynchronous-programming-for-xna-games-with-squaredtask/comment-page-1#comment-315</link>
		<dc:creator>Concurrency on a single thread &#8211; 22th Edition &#124; ArcadePortal</dc:creator>
		<pubDate>Wed, 02 Sep 2009 02:28:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.luminance.org/?p=465#comment-315</guid>
		<description>[...] Gadd&#8217;s shows a elegant solution to this problem in C# using custom enumerators in an article aptly named &#8220;Asynchronous programming for xna games with [...]</description>
		<content:encoded><![CDATA[<p>[...] Gadd&#8217;s shows a elegant solution to this problem in C# using custom enumerators in an article aptly named &#8220;Asynchronous programming for xna games with [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

