<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://xna-uk.net/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>RandomChaos</title><link>http://xna-uk.net/blogs/randomchaos/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Generic XNA - Threading for Windows &amp; XBox 360</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/08/15/generic-xna-threading-for-windows-amp-xbox-360.aspx</link><pubDate>Fri, 15 Aug 2008 10:55:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:2126</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=2126</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/08/15/generic-xna-threading-for-windows-amp-xbox-360.aspx#comments</comments><description>&lt;p&gt;OK, I went back to my old threading code to port it to the 360 so I could start playing about with processor affinity (as promised last time) and found the compact framework for the 360 was a bit thin on the ground for parameterized threading, so I have re written the whole thing and it will now play on both the PC and the 360 and also included processor affinity for your 360 projects and there is very little interface change so if you were using the code for your Windows games you can just replace the threading code wit this and you wont have to alter any of your calls to it.&lt;/p&gt;
&lt;p&gt;So, what did I do??&lt;/p&gt;
&lt;p&gt;Pretty much ripped the lot out and started again. The ThreadManagers innards have been totally re done, now instead of having a shed load of Dictionaries holding mutex&amp;#39;s and the like we now just have three, one for the thread starters, one for the threaded code and one for the threads them selves.&lt;/p&gt;
&lt;div style="BORDER-RIGHT:black 1pt solid;BORDER-TOP:black 1pt solid;OVERFLOW:auto;BORDER-LEFT:black 1pt solid;BORDER-BOTTOM:black 1pt solid;HEIGHT:200pt;"&gt;&lt;pre class="code"&gt;    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;This is the Thread manager.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public class &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadManager &lt;/span&gt;: &lt;span style="COLOR:#2b91af;"&gt;GameComponent
    &lt;/span&gt;{

        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;List of ThreadStart used to start the treads.
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;private &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;ThreadStart&lt;/span&gt;&amp;gt; threadStarters = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;ThreadStart&lt;/span&gt;&amp;gt;();
        &lt;span style="COLOR:blue;"&gt;private &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;ThreadCodeObj&lt;/span&gt;&amp;gt; threadedCodeList = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;ThreadCodeObj&lt;/span&gt;&amp;gt;();
        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;List of runnign threads.
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;private &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;Thread&lt;/span&gt;&amp;gt; threads = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;Thread&lt;/span&gt;&amp;gt;();

        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Managers GameTime to be passed onto the threads.
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;static &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;GameTime &lt;/span&gt;gameTime;
        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;ctor
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name=&amp;quot;game&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="COLOR:green;"&gt;Calling game class&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;&amp;lt;/param&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public &lt;/span&gt;ThreadManager(&lt;span style="COLOR:#2b91af;"&gt;Game &lt;/span&gt;game) : &lt;span style="COLOR:blue;"&gt;base&lt;/span&gt;(game)
        { }

        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Overiden Update call, loads manager gameTime varaible.
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name=&amp;quot;gameTime&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public override void &lt;/span&gt;Update(&lt;span style="COLOR:#2b91af;"&gt;GameTime &lt;/span&gt;gameTime)
        {
            &lt;span style="COLOR:#2b91af;"&gt;ThreadManager&lt;/span&gt;.gameTime = gameTime;

            &lt;span style="COLOR:blue;"&gt;for &lt;/span&gt;(&lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;t = 0; t &amp;lt; threadedCodeList.Count; t++)
                threadedCodeList[t].Update(gameTime);

            &lt;span style="COLOR:blue;"&gt;base&lt;/span&gt;.Update(gameTime);
        }

        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Method to add a thread to the maanger.
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name=&amp;quot;threadCode&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="COLOR:green;"&gt;Code to be executed in the thread.&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;threadInterval&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="COLOR:green;"&gt;Time period between each call in miliseconds&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="COLOR:green;"&gt;Index of thread, first one added will be 0 next 1 etc..&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;&amp;lt;/returns&amp;gt;
&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;#if &lt;/span&gt;XBOX
        &lt;span style="COLOR:gray;"&gt;public int AddThread(ThreadCode threadCode, int threadInterval,int affinityIndex)
&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;#else
        public int &lt;/span&gt;AddThread(&lt;span style="COLOR:#2b91af;"&gt;ThreadCode &lt;/span&gt;threadCode, &lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;threadInterval)
&lt;span style="COLOR:blue;"&gt;#endif
        &lt;/span&gt;{
            &lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;retVal = threads.Count;

&lt;span style="COLOR:blue;"&gt;#if &lt;/span&gt;XBOX
            &lt;span style="COLOR:gray;"&gt;ThreadCodeObj thisThread = new ThreadCodeObj(threadCode, threadInterval,affinityIndex);
&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;#else
            &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadCodeObj &lt;/span&gt;thisThread = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadCodeObj&lt;/span&gt;(threadCode, threadInterval);
&lt;span style="COLOR:blue;"&gt;#endif

            &lt;/span&gt;threadedCodeList.Add(threadedCodeList.Count, thisThread);
            threadStarters.Add(threadStarters.Count, &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadStart&lt;/span&gt;(thisThread.Worker));
            threads.Add(threads.Count, &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Thread&lt;/span&gt;(threadStarters[threads.Count]));

            threads[threads.Count - 1].Start();

            &lt;span style="COLOR:blue;"&gt;return &lt;/span&gt;retVal;
        }

        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Method to kill a single thread.
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name=&amp;quot;index&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public void &lt;/span&gt;KillThread(&lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;index)
        {
            threadedCodeList[index].KillThread(threads[index]);
        }

        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Method to start a thread
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name=&amp;quot;threadCode&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;threadInterval&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;index&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public void &lt;/span&gt;StartThread(&lt;span style="COLOR:#2b91af;"&gt;ThreadCode &lt;/span&gt;threadCode, &lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;threadInterval, &lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;index)
        {
            &lt;span style="COLOR:blue;"&gt;if &lt;/span&gt;(threadedCodeList[index].stopThread)
            {
                threads[index] = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Thread&lt;/span&gt;(threadStarters[index]);
                threadedCodeList[index].stopThread = &lt;span style="COLOR:blue;"&gt;false&lt;/span&gt;;
                threads[index].Start();
            }
        }

        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Method to tidy up unfinished threads.
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name=&amp;quot;disposing&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;protected override void &lt;/span&gt;Dispose(&lt;span style="COLOR:blue;"&gt;bool &lt;/span&gt;disposing)
        {
            &lt;span style="COLOR:blue;"&gt;for &lt;/span&gt;(&lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;t = 0; t &amp;lt; threads.Count; t++)
                KillThread(t);

            &lt;span style="COLOR:blue;"&gt;base&lt;/span&gt;.Dispose(disposing);
        }
    }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;The real change has been in the ThreadCodeObj, this now holds most of your threading information. &lt;/p&gt;
&lt;div style="BORDER-RIGHT:black 1pt solid;BORDER-TOP:black 1pt solid;OVERFLOW:auto;BORDER-LEFT:black 1pt solid;BORDER-BOTTOM:black 1pt solid;HEIGHT:200pt;"&gt;&lt;pre class="code"&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;This is the delegate to be used for passing the code to be called in the tread.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;gameTime&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="COLOR:green;"&gt;GameTime&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public delegate void &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadCode&lt;/span&gt;(&lt;span style="COLOR:#2b91af;"&gt;GameTime &lt;/span&gt;gameTime);

    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;This class holds the required data for the code to be called in the thread.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;internal class &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadCodeObj
    &lt;/span&gt;{
        &lt;span style="COLOR:blue;"&gt;public &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadCode &lt;/span&gt;CodeToCall = &lt;span style="COLOR:blue;"&gt;null&lt;/span&gt;;

        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Mutex to stop thread clashes.
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;private static &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Mutex &lt;/span&gt;mutex = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Mutex&lt;/span&gt;();
        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Used to make the thread wait.
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;private &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ManualResetEvent &lt;/span&gt;threadStopEvent = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ManualResetEvent&lt;/span&gt;(&lt;span style="COLOR:blue;"&gt;false&lt;/span&gt;);
        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Bool to control imediate stopping of thread loop.
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;        
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public bool &lt;/span&gt;stopThread = &lt;span style="COLOR:blue;"&gt;false&lt;/span&gt;;
        &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Interval thread will wait befoer next cycle.
        &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;        
        &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;private int &lt;/span&gt;threadIntervals = 1;

&lt;span style="COLOR:blue;"&gt;#if &lt;/span&gt;XBOX 
        &lt;span style="COLOR:gray;"&gt;int processorAffinity;
&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;#endif

        public &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;GameTime &lt;/span&gt;gameTime;

&lt;span style="COLOR:blue;"&gt;#if &lt;/span&gt;XBOX
        &lt;span style="COLOR:gray;"&gt;public ThreadCodeObj(ThreadCode code,int interval,int affinity)
        {
            CodeToCall = code;
            threadIntervals = interval;
            processorAffinity = affinity;
        }
&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;#else
        public &lt;/span&gt;ThreadCodeObj(&lt;span style="COLOR:#2b91af;"&gt;ThreadCode &lt;/span&gt;code, &lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;interval)
        {
            CodeToCall = code;
            threadIntervals = interval;
        }
&lt;span style="COLOR:blue;"&gt;#endif
        public void &lt;/span&gt;Update(&lt;span style="COLOR:#2b91af;"&gt;GameTime &lt;/span&gt;gameTime)
        {
            &lt;span style="COLOR:blue;"&gt;this&lt;/span&gt;.gameTime = gameTime;
        }
        &lt;span style="COLOR:blue;"&gt;public void &lt;/span&gt;Worker()
        {
&lt;span style="COLOR:blue;"&gt;#if &lt;/span&gt;XBOX
            &lt;span style="COLOR:gray;"&gt;Thread.CurrentThread.SetProcessorAffinity(new int[] { processorAffinity });
&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;#endif
            do
            &lt;/span&gt;{
                &lt;span style="COLOR:blue;"&gt;try
                &lt;/span&gt;{
                    mutex.WaitOne();

                    &lt;span style="COLOR:blue;"&gt;if &lt;/span&gt;(gameTime != &lt;span style="COLOR:blue;"&gt;null&lt;/span&gt;)
                        CodeToCall(gameTime);

                }
                &lt;span style="COLOR:blue;"&gt;finally
                &lt;/span&gt;{
                    mutex.ReleaseMutex();
                }
            }
            &lt;span style="COLOR:blue;"&gt;while &lt;/span&gt;(!threadStopEvent.WaitOne(threadIntervals, &lt;span style="COLOR:blue;"&gt;false&lt;/span&gt;) &amp;amp;&amp;amp; !stopThread);
        }
        &lt;span style="COLOR:blue;"&gt;public void &lt;/span&gt;KillThread(&lt;span style="COLOR:#2b91af;"&gt;Thread &lt;/span&gt;thread)
        {
            &lt;span style="COLOR:blue;"&gt;if &lt;/span&gt;(!stopThread)
            {
                mutex.WaitOne();
                stopThread = &lt;span style="COLOR:blue;"&gt;true&lt;/span&gt;;
                thread.Join(0);
                mutex.ReleaseMutex();
            }
        }
    }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;At the top of the source file I have also included an affinity map so you know what processors you have access to, or should be using. XNA does not stop you from using a CPU/Core but you don&amp;#39;t want to be running your threads on CPU cores that are already busy, it&amp;#39;s why you are threading in the first place, because you have to.&lt;/p&gt;
&lt;div style="BORDER-RIGHT:black 1pt solid;BORDER-TOP:black 1pt solid;OVERFLOW:auto;BORDER-LEFT:black 1pt solid;BORDER-BOTTOM:black 1pt solid;HEIGHT:100pt;"&gt;&lt;pre class="code"&gt;&lt;span style="COLOR:blue;"&gt;#if &lt;/span&gt;XBOX
    &lt;span style="COLOR:gray;"&gt;///
    /// Processor affinity map.
    /// Index CPU CORE Comment
    /// -----------------------------------------------------------------------
    ///   0    1    1  Please avoid using. (used by 360)
    ///   1    1    2  Game runs here by default, so avoid this one too.
    ///   2    2    1  Please avoid using. (used by 360)
    ///   3    2    2  Part of Guide and Dashboard live here so usable in game.
    ///   4    3    1  Live market place downloads use this so usable in game.
    ///   5    3    2  Part of Guide and Dashboard live here so usable in game.
    /// -----------------------------------------------------------------------  
    ///
&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;#endif&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;The sample solution is the same as the last one, but just uses this method now and you can download this &lt;a class="" title="Source" href="http://xna-uk.net/files/folders/randomchaos/entry2128.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=2126" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Source/default.aspx">Source</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XBox+360/default.aspx">XBox 360</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+2.0/default.aspx">XNA 2.0</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Threading/default.aspx">Threading</category></item><item><title>A visible collection of particles of water or ice suspended in the air, usually at an elevation above the earth's surface in XNA</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/08/08/a-visible-collection-of-particles-of-water-or-ice-suspended-in-the-air-usually-at-an-elevation-above-the-earth-s-surface-in-xna.aspx</link><pubDate>Fri, 08 Aug 2008 11:30:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:2099</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=2099</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/08/08/a-visible-collection-of-particles-of-water-or-ice-suspended-in-the-air-usually-at-an-elevation-above-the-earth-s-surface-in-xna.aspx#comments</comments><description>&lt;p&gt;Well if I had just put, &amp;quot;Another Cloud Clip!&amp;quot; you would not have looked at this post. Anyway, her is some more of the same, but not quite the same, well, almost the same. In this clip I show multiple cloud layers each with differing tones of gray (I think it gives the cloud fields depth), also a splattering of around 100 clouds and then the cloud fields again but with some basic formation and dissipation effects in there.&lt;/p&gt;
&lt;p&gt;[Go to the site to view embedded media]&lt;/p&gt;
&lt;p&gt;Now as ever it looks a little laggy, I can&amp;#39;t help this, but what I intend to do next time is to try and dig out my camera and film the cloud system off my TV to show how quick it is in my XBox 360, I would have done that this time around but it is dead... No RROD, it powered up, I get the normal green lights, one for power the other for my controller but I get no sound or video, which kind of detracts from game play a bit... &lt;/p&gt;
&lt;p&gt;So next time,&amp;nbsp; I hope to have a 360 clip up, quality will suck as it will be taken on my hand held camera, but with any luck you will see it run without lag.&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=2099" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Clouds/default.aspx">Clouds</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+2.0/default.aspx">XNA 2.0</category></item><item><title>Gamefest 2008</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/08/08/gamefest-2008.aspx</link><pubDate>Fri, 08 Aug 2008 10:32:05 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:2098</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=2098</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/08/08/gamefest-2008.aspx#comments</comments><description>&lt;p&gt;Well managed to go! It was great, well the bits I saw as I didn&amp;#39;t get there till around 11:00, but got to see most of Frank Savages talk on Threading in XNA which was interesting and threw up a few tips, also the talk on Live networking by Mitch Walker was very insightful. &lt;/p&gt; &lt;p&gt;Now one talk that I did find changed my out look on an area of XNA was Frank Savages talk on the Content Pipeline. Now I knew you could do a fair bit with it, have played a little with it my self, but WOW! There is SO much in there to play with. An example he gave was when creating your levels in your modelling tool, use spheres for markers in the level for things like spawn points, event triggers etc.. label these spheres so you know what they are pertaining to, then you can write your own content override to get this data out of the model, use the centre of the sphere to mark the point, store it in an array of points and then store it in the tag of the Model (remember to remove this excess data prior to passing it on to the base class) It was so obvious yet I had not even thought about it, so much other stuff we could do in there too, I have been using it to store bounding box data as well as vertex data (not needed since 2.0 I don&amp;#39;t think), but this could also include collision normals, further bounding data, breaking the model up into a number of spheres for greater collision accuracy, and that&amp;#39;s just models, anything else that falls under the content project can be manipulated, this is still all pretty basic stuff, but I am sure you can imagine how much control we now have over our content.&lt;/p&gt; &lt;p&gt;Well after all that exciting stuff, we had a break for lunch, I then went to the Community Games on XBox Live talk (after debating with myself if I should stay for the XBLA talk) and it was quite informative, nothing there I didn&amp;#39;t really know or heard about already, other than the, &amp;quot;If your games kills anyone who plays it, it&amp;#39;s your fault!&amp;quot; bit, well they didn&amp;#39;t put it quite like that, but you are liable for your game I guess, being the publisher and the developer all in one... As you can imagine a lot of people in the room seemed to be industry folk and so there questions were more around the realms of IPR infringement and how they will now stop there developers moon lighting to make money with XNA. Oh and someone asked about people writing XBox time bomb&amp;#39;s, malicious code that would get through the peer review as it only goes off at Christmas or Halloween, what I find odd about that is the limited access you have to both the network and the hardware on the 360, guess you could fill a hard drive or maybe flash up naughty pictures for a day, but I guess that&amp;#39;s going to get you and your 360&amp;#39;s mac address banned&amp;nbsp; for life....but then I guess there are some odd folk out there...you know who I mean....yes you there...no, not you, you...&lt;/p&gt; &lt;p&gt;So then came the party after....I say party, but was just a gathering of the attendees for free booze and food, oh a blast on Geometry Wars and other titles around the room to, no Schitzoid which I found odd, would have thought they would have had a copy of that running at an XNA conference. Met some guys from &lt;a href="http://beatnickgames.co.uk/" target="_blank"&gt;Beatnick Games Ltd&lt;/a&gt; (Hi Robin) who are a start up writing a game in XNA, think they want us to play test the beta, so guess we are waiting for them to give us access now so we can rip it apart...&lt;/p&gt; &lt;p&gt;Shame I missed the keynote and the What&amp;#39;s new in XNA 3.0 talk, would have been good to catch those.&lt;/p&gt; &lt;p&gt;All in all good stuff, I think I may now have an addiction to games dev conferences....ah, my personality flaws shining out again, first XNA now this....&lt;/p&gt; &lt;p&gt;Thanks to Leaf and Robin for sorting stuff out and making sure I did not get lost on the tube ride back to St Pancs...&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=2098" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Conferences/default.aspx">Conferences</category></item><item><title>Anyone getting sick of clouds yet?</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/07/18/anyone-getting-sick-of-clouds-yet.aspx</link><pubDate>Fri, 18 Jul 2008 14:45:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:2046</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=2046</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/07/18/anyone-getting-sick-of-clouds-yet.aspx#comments</comments><description>&lt;p&gt;[Go to the site to view embedded media]&lt;/p&gt;
&lt;p&gt;So, here is the system (still not finished) with the skysphere and&amp;nbsp;my infantile physics system. Again footage taken on my poop laptop so bit slow, but on my 360 it&amp;#39;s a steady 60FPS. &lt;/p&gt;
&lt;p&gt;I think the flying ship gives a better perspective than the terrain, also I have altered the cloud shader to take into account the time of day, you will see at night fall the clouds are no longer bright white.&lt;/p&gt;
&lt;p&gt;As ever comment and ratings welcome.&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=2046" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/SkySphere/default.aspx">SkySphere</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Clouds/default.aspx">Clouds</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Physics/default.aspx">Physics</category></item><item><title>Speech in you Windows XNA Games</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/07/17/speech-in-you-windows-xna-games.aspx</link><pubDate>Thu, 17 Jul 2008 13:44:53 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:2042</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=2042</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/07/17/speech-in-you-windows-xna-games.aspx#comments</comments><description>&lt;p&gt;I found this while messing about with the media player in XNA, just noticed there is some legacy code in the download from it :( Anyway, how to get speech into your games, it&amp;#39;s dead easy really, you just need to have .NET Framework 3.0 and you have all you need. &lt;/p&gt; &lt;p&gt;&lt;strong&gt;NOTE: You don&amp;#39;t need XNA 3.0 for this just the .NET 3.0 Framework on your system.&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;First off you need to add the System.Speech assembly to your project references, then in your Game1.cs add the following:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;using &lt;/span&gt;System.Speech.Synthesis;&lt;/pre&gt;
&lt;p&gt;and in your Game1 class add:&lt;/p&gt;
&lt;p&gt;&lt;span style="color:#2b91af;"&gt;SpeechSynthesizer &lt;/span&gt;synth = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;SpeechSynthesizer&lt;/span&gt;();&lt;/p&gt;
&lt;p&gt;Now, all you have to do is call the synth.SpeackAsync() method passing the text you want to have spoken. I used this:&lt;/p&gt;&lt;pre class="code"&gt;synth.SpeakAsync(&lt;span style="color:#a31515;"&gt;&amp;quot;Hello, Welcome to the Random Chaos Generic X N A Speech Sample.&amp;quot; &lt;/span&gt;+
                                &lt;span style="color:#a31515;"&gt;&amp;quot; This sample is hosted by; the X N A U K User Group. Thanks &amp;quot; &lt;/span&gt;+
                                &lt;span style="color:#a31515;"&gt;&amp;quot;to Leaf and Robin for letting me host my blog here and I hope &amp;quot; &lt;/span&gt;+
                                &lt;span style="color:#a31515;"&gt;&amp;quot;you find my samples useful.&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;This will use the default voice that is set up on your PC (see Control Panel-&amp;gt;Speech). But you can also access the other two voice types that ship with the OS (XP, not sure about Vista). To do this try any one of these:&lt;/p&gt;&lt;pre class="code"&gt;synth.SelectVoice(&lt;span style="color:#a31515;"&gt;&amp;quot;LH Michael&amp;quot;&lt;/span&gt;);
synth.SelectVoice(&lt;span style="color:#a31515;"&gt;&amp;quot;LH Michelle&amp;quot;&lt;/span&gt;);
synth.SelectVoice(&lt;span style="color:#a31515;"&gt;&amp;quot;Microsoft Sam&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;I prefer &amp;quot;LH Michael&amp;quot;, reminds me of Stephen Hawking and I think it&amp;#39;s a little clearer than the other two.&lt;/p&gt;
&lt;p&gt;I know, not the best chat in the world, but it&amp;#39;s free lol&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=2042" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Speech/default.aspx">Speech</category></item><item><title>Yes, more clouds...</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/07/03/yes-more-clouds.aspx</link><pubDate>Thu, 03 Jul 2008 21:59:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:2020</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=2020</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/07/03/yes-more-clouds.aspx#comments</comments><description>&lt;p&gt;[Go to the site to view embedded media]&lt;/p&gt;
&lt;p&gt;Yes, been playing some more with the clouds. I have had the last clip playing on the 360 and it not drop below 60 fps, on my laptop I get about 7fps, and yes, my laptop sux!!&lt;/p&gt;
&lt;p&gt;So I have now put it with my dynamic sky sphere, and I think it looks pretty good. In the clip I am showing a few large clouds in a huge bounding volume against the sky sphere, then a large cloud field against the sky sphere showing the transfer from day to night and back again. I have not implemented lighting on the clouds but I think it still looks nice, the final bit of the clip is both bits together with an added mushroom cloud in the middle.&lt;/p&gt;
&lt;p&gt;Still much to do and as ever, very little time, but I am getting there I think, all though slowly :)&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=2020" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Clouds/default.aspx">Clouds</category></item><item><title>3D Clouds in XNA - Another Update</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/06/12/3d-clouds-in-xna-another-update.aspx</link><pubDate>Thu, 12 Jun 2008 22:41:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:1993</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=1993</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/06/12/3d-clouds-in-xna-another-update.aspx#comments</comments><description>&lt;p&gt;[Go to the site to view embedded media]&lt;/p&gt;
&lt;p&gt;Well I have kind of been letting this code sit for a while, not looked at it as of late, then this week decided to have another play with it, kind of hoping I can put it to bed.&lt;/p&gt;
&lt;p&gt;So I have not started to use Ninians sprite sheet, the sheet is full of alphas, so instead of getting them off the sheet and then just sending them to the screen, set the base colour of the sprite then multiply its alpha by the RGB returned from the sprite sheet. This I think gives a more natural cloud look. I still have some performance issues, but have managed to speed up the draw order sort and that has helped a little. I have also put in some terrain so you have a point of reference, was a bit odd just looking at clouds hanging there in the sky..&lt;/p&gt;
&lt;p&gt;I still have tones I want to do with this yet. I think the next thing will be imposters, then an editor so you can create a cloud field or set of clouds and be able to save them to disk and re use them as an when you like. Naturally more optimizations and some lighting in the shader. &lt;/p&gt;
&lt;p&gt;Hop you like the clip, ratings, and comments are welcome as ever.&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=1993" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Clouds/default.aspx">Clouds</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+2.0/default.aspx">XNA 2.0</category></item><item><title>Generic XNA - Threading for Windows</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/06/01/generic-xna-threading-for-windows.aspx</link><pubDate>Sun, 01 Jun 2008 18:23:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:1978</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=1978</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/06/01/generic-xna-threading-for-windows.aspx#comments</comments><description>&lt;p&gt;&lt;a class="" href="http://xna-uk.net/photos/screen_shots/images/1979/original.aspx"&gt;&lt;img height="200" alt="" hspace="150" src="http://xna-uk.net/photos/screen_shots/images/1979/original.aspx" width="300" border="0" /&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Well I have done some basic threading in my job, basic stuff and thought I would try and put the basic threading I have done into an XNA Game Component. I am not saying this is how you should do your threading, my method is certainly not the only way to thread processes either and more than likely not the best. I think though that if you are new to development and you want to thread part of your game then I think this may be of some use to you.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a thread?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;OK, there are a few links out there that describe what a thread is better than I could, so here are a few links for reference:-&lt;/p&gt;
&lt;p&gt;&lt;a href="http://research.microsoft.com/~birrell/papers/ThreadsCSharp.pdf" target="_blank"&gt;Research Microsoft Andrew D. Birrel&lt;/a&gt; (not read this yet, looks v good though)&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa645740(VS.71).aspx" target="_blank"&gt;MSDN&lt;/a&gt; (Again, I should read this too...)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ThreadManager&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is a simple manager, you can pass methods to it that you want to have processed in a thread of there own. You can have as many threads as you like, BUT, it&amp;#39;s probably best to keep your threads down to as few as possible, don&amp;#39;t go threading processes because you can.&lt;/p&gt;
&lt;p&gt;So, on to the code. The ThreadManager class is derived from (as most my samples) from GameComponent. The threading method I use requires four components, ParameterizedThreadStart if you don&amp;#39;t need to pass parameters to your thread start method then you can use ThreadStart; these are how we bind our method to a Thread, Thread this is the actual thread class used to start and stop the thread, ManualResetEvent this class is used to manage the call interval for the thread and the Mutex class which is used to ensure there are no thread clashes. I wanted to be able to add as many threads as I wanted in my ThreadManager so I have put these class instances into Dictionaries. Also in this implementation I wanted to be able to manage how often the thread code was ran and also to be able to kill a thread at will. To do this I added two other dictionaries to hold what threads I want to stop and one for the interval between thread calls.&lt;/p&gt;
&lt;div style="BORDER-RIGHT:black 1pt solid;BORDER-TOP:black 1pt solid;OVERFLOW:auto;BORDER-LEFT:black 1pt solid;BORDER-BOTTOM:black 1pt solid;HEIGHT:200pt;"&gt;&lt;pre class="code"&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;This is the Thread manager.
&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public class &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadManager &lt;/span&gt;: &lt;span style="COLOR:#2b91af;"&gt;GameComponent
&lt;/span&gt;{
    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;List of ParameterizedThreadStart used to start the treads.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;private &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;ParameterizedThreadStart&lt;/span&gt;&amp;gt; threadStarters = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;ParameterizedThreadStart&lt;/span&gt;&amp;gt;();
    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;List of runnign threads.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;private &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;,&lt;span style="COLOR:#2b91af;"&gt;Thread&lt;/span&gt;&amp;gt; threads = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;,&lt;span style="COLOR:#2b91af;"&gt;Thread&lt;/span&gt;&amp;gt;();
    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Used to make the thread wait.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public static &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;ManualResetEvent&lt;/span&gt;&amp;gt; threadStopEvent = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;ManualResetEvent&lt;/span&gt;&amp;gt;();
    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Mutex&amp;#39;s to stop thread clashes.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public static &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;Mutex&lt;/span&gt;&amp;gt; mutexs = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;Mutex&lt;/span&gt;&amp;gt;();
    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;List of bools to control imediate stopping of thread loops.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public static &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:blue;"&gt;bool&lt;/span&gt;&amp;gt; stopThreads = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:blue;"&gt;bool&lt;/span&gt;&amp;gt;();
    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;List of intervals threads will wait befoer next cycle.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public static &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; threadIntervals = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;, &lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt;();
    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Managers GameTime to be passed onto the threads.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;static &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;GameTime &lt;/span&gt;gameTime;

    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;ctor
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;game&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="COLOR:green;"&gt;Calling game class&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public &lt;/span&gt;ThreadManager(&lt;span style="COLOR:#2b91af;"&gt;Game &lt;/span&gt;game) : &lt;span style="COLOR:blue;"&gt;base&lt;/span&gt;(game)
    { }

    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Overiden Update call, loads manager gameTime varaible.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;gameTime&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public override void &lt;/span&gt;Update(&lt;span style="COLOR:#2b91af;"&gt;GameTime &lt;/span&gt;gameTime)
    {
        &lt;span style="COLOR:#2b91af;"&gt;ThreadManager&lt;/span&gt;.gameTime = gameTime;

        &lt;span style="COLOR:blue;"&gt;base&lt;/span&gt;.Update(gameTime);
    }

    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Method to add a thread to the maanger.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;threadCode&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="COLOR:green;"&gt;Code to be executed in the thread.&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name=&amp;quot;threadInterval&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="COLOR:green;"&gt;Time period between each call in miliseconds&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="COLOR:green;"&gt;Index of thread, first one added will be 0 next 1 etc..&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;&amp;lt;/returns&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public int &lt;/span&gt;AddThread(&lt;span style="COLOR:#2b91af;"&gt;ThreadCode &lt;/span&gt;threadCode, &lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;threadInterval)
    {
        &lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;retVal = threads.Count;

        threadStarters.Add(threadStarters.Count, &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ParameterizedThreadStart&lt;/span&gt;(ThreadWorker));
        threads.Add(threads.Count, &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Thread&lt;/span&gt;(threadStarters[threads.Count]));
        threadStopEvent.Add(threadStopEvent.Count, &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ManualResetEvent&lt;/span&gt;(&lt;span style="COLOR:blue;"&gt;false&lt;/span&gt;));
        mutexs.Add(mutexs.Count, &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Mutex&lt;/span&gt;());
        threadIntervals.Add(threadIntervals.Count, threadInterval);
        stopThreads.Add(stopThreads.Count, &lt;span style="COLOR:blue;"&gt;false&lt;/span&gt;);

        threads[threads.Count-1].Start(&lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadCodeObj&lt;/span&gt;(threadCode,threads.Count-1));

        &lt;span style="COLOR:blue;"&gt;return &lt;/span&gt;retVal;
    }

    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Method to kill a single thread.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;index&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public void &lt;/span&gt;KillThread(&lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;index)
    {
        mutexs[index].WaitOne();
        stopThreads[index] = &lt;span style="COLOR:blue;"&gt;true&lt;/span&gt;;
        threads[index].Join(0);
        mutexs[index].ReleaseMutex();
    }
    
    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Method to start a thread
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;threadCode&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name=&amp;quot;threadInterval&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name=&amp;quot;index&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public void &lt;/span&gt;StartThread(&lt;span style="COLOR:#2b91af;"&gt;ThreadCode &lt;/span&gt;threadCode,&lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;threadInterval,&lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;index)
    {
        &lt;span style="COLOR:blue;"&gt;if &lt;/span&gt;(threads[index].ThreadState == &lt;span style="COLOR:#2b91af;"&gt;ThreadState&lt;/span&gt;.Stopped)
        {
            threads[index] = &lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;Thread&lt;/span&gt;(threadStarters[index]);
            threadIntervals[index] =  threadInterval;
            stopThreads[index] = &lt;span style="COLOR:blue;"&gt;false&lt;/span&gt;;
            threads[index].Start(&lt;span style="COLOR:blue;"&gt;new &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadCodeObj&lt;/span&gt;(threadCode, index));
        }
    }

    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;This is the method that is called per thread.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;ThreadCodeObject&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="COLOR:green;"&gt;Instance of ThreadCodeObj to execute.&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;private static void &lt;/span&gt;ThreadWorker(&lt;span style="COLOR:blue;"&gt;object &lt;/span&gt;ThreadCodeObject)
    {
        &lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;index = ((&lt;span style="COLOR:#2b91af;"&gt;ThreadCodeObj&lt;/span&gt;)ThreadCodeObject).threadIndex;

        &lt;span style="COLOR:blue;"&gt;do
        &lt;/span&gt;{
            &lt;span style="COLOR:blue;"&gt;try
            &lt;/span&gt;{
                mutexs[index].WaitOne();
                &lt;span style="COLOR:blue;"&gt;if &lt;/span&gt;(gameTime != &lt;span style="COLOR:blue;"&gt;null&lt;/span&gt;)
                    ((&lt;span style="COLOR:#2b91af;"&gt;ThreadCodeObj&lt;/span&gt;)ThreadCodeObject).CodeToCall(gameTime);

            }
            &lt;span style="COLOR:blue;"&gt;finally
            &lt;/span&gt;{
                mutexs[index].ReleaseMutex();
            }
        }
        &lt;span style="COLOR:blue;"&gt;while &lt;/span&gt;(!threadStopEvent[index].WaitOne(threadIntervals[index], &lt;span style="COLOR:blue;"&gt;false&lt;/span&gt;) &amp;amp;&amp;amp; !stopThreads[index]);
    }

    &lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;Method to tidy up unfinished threads.
    &lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;disposing&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;protected override void &lt;/span&gt;Dispose(&lt;span style="COLOR:blue;"&gt;bool &lt;/span&gt;disposing)
    {
        &lt;span style="COLOR:blue;"&gt;for &lt;/span&gt;(&lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;t = 0; t &amp;lt; threads.Count; t++)
            KillThread(t);

        &lt;span style="COLOR:blue;"&gt;base&lt;/span&gt;.Dispose(disposing);
    }
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;As you can see in the code snippet above, there are 4 methods that are not native to the GameComponent, AddThread, KillThread, StartThread, ThreadWorker.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;AddThread&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Well, guess what this method does...yep, adds a thread to the manager. So each thread needs one of the base thread elements to, I also set the interval and if the thread is stopped. I set the flag to say this thread has been stopped, then call the threads Join method, this stops (blocks) this thread from running until the thread terminates, this won&amp;#39;t happen until the threads loop in ThreadWorker exits and is why I set the stop flag for the thread so that it will terminate. You will see in this call I use another class ThreadCodeObj.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ThreadCodeObj&lt;/strong&gt;&lt;/p&gt;
&lt;div style="BORDER-RIGHT:black 1pt solid;BORDER-TOP:black 1pt solid;OVERFLOW:auto;BORDER-LEFT:black 1pt solid;BORDER-BOTTOM:black 1pt solid;HEIGHT:100pt;"&gt;&lt;pre class="code"&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;This is the delegate to be used for passing the code to be called in the tread.
&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
/// &amp;lt;param name=&amp;quot;gameTime&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="COLOR:green;"&gt;GameTime&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;&amp;lt;/param&amp;gt;
&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;public delegate void &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadCode&lt;/span&gt;(&lt;span style="COLOR:#2b91af;"&gt;GameTime &lt;/span&gt;gameTime);

&lt;span style="COLOR:gray;"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="COLOR:green;"&gt;This class holds the required data for the code to be called in the thread.
&lt;/span&gt;&lt;span style="COLOR:gray;"&gt;/// &amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;internal class &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadCodeObj
&lt;/span&gt;{
    &lt;span style="COLOR:blue;"&gt;public &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;ThreadCode &lt;/span&gt;CodeToCall = &lt;span style="COLOR:blue;"&gt;null&lt;/span&gt;;
    &lt;span style="COLOR:blue;"&gt;public int &lt;/span&gt;threadIndex = -1;

    &lt;span style="COLOR:blue;"&gt;public &lt;/span&gt;ThreadCodeObj(&lt;span style="COLOR:#2b91af;"&gt;ThreadCode &lt;/span&gt;code, &lt;span style="COLOR:blue;"&gt;int &lt;/span&gt;index)
    {
        CodeToCall = code;
        threadIndex = index;
    }
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;This class stores the code to be called in the thread and the index of the thread it is to be used in.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;KIllThread&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Yep, you got it, kills a running thread. First off the treads mutex calls it&amp;#39;s WaitOne method, this method holds this thread up until it&amp;#39;s free to do it&amp;#39;s work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;StartThread&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Restarts a killed thread. First check if this thread has been stopped, if it has then re initialise the threads elements.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ThreadWorker&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Well this I guess is where it all happens, first we get the index of the thread we are in, then we kick off the threads loop, get the mutex for this thread to tell us when it is safe to call the delegate, then we make sure the gameTime instance is not null and call the delegate. We then get the mutex for this thread to signal that we are done and then check if we need to leave the loop and if not wait for the given interval for this thread index.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;strong&gt;Implementation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So as with most of my samples (I like to think) it&amp;#39;s simple to add a method to a thread. First of all set up the ThreadManager and add it to the Game.Components list, now simply call the ThreadManager.AddThread method, passing it the method you want to put in the thread via the ThreadCode delegate along with the interval you want for the thread. I guess most of the threads you will create will have a time span of 0 so they are executed immediately.&lt;/p&gt;
&lt;p&gt;You will also notice I have not bothered with &lt;a href="http://en.wikipedia.org/wiki/Processor_affinity" target="_blank"&gt;affinity&lt;/a&gt; (the ability to request the processor the thread will run on) in this sample, this is because on a PC, you wont know how many CPU&amp;#39;s your application will be running on so it&amp;#39;s probably best left up to the OS to sort it out. &lt;/p&gt;
&lt;p&gt;My next post on threading will be for the XBox, it will be almost identical with this source but will have affinity included, we know where we stand with the 360, we know what processors, how many cores and which ones we can put threads on.&lt;/p&gt;
&lt;p&gt;The sample code shows this working in a very simplistic manor, I simply add three methods to the thread manager that populate a variable that holds the game time each time it is called. You can kill the thread and then re start them with an interval of 0.&lt;/p&gt;
&lt;p&gt;Source solution can be found &lt;a class="" href="http://xna-uk.net/files/folders/randomchaos/entry1980.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=1978" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Source/default.aspx">Source</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+2.0/default.aspx">XNA 2.0</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Threading/default.aspx">Threading</category></item><item><title>Generic XNA - Movies in your Windows games</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/05/28/generic-xna-movies-in-your-windows-games.aspx</link><pubDate>Wed, 28 May 2008 14:38:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:1962</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=1962</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/05/28/generic-xna-movies-in-your-windows-games.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://xna-uk.net/photos/screen_shots/images/1961/original.aspx" target="_blank"&gt;&lt;img height="200" src="http://xna-uk.net/photos/screen_shots/images/1961/original.aspx" width="268" align="right" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Well, I started looking into this after a friend of mine asked if it could be done. I remembered the other week on Ziggyware that someone got a web cam to stream in an XNA app and so thought that the principle should be the same, taking the output, rendering it as a texture then applying it as you required. So started to try and do it myself, well I came a cropper as the examples I came across were for the old MDX and not XNA (I am not bright enough to do a full port), having to use unsafe code to get handles to the Game.Graphics device etc... but then I came across &lt;a href="http://www.codeplex.com/XNADSPlayer" target="_blank"&gt;this&lt;/a&gt; post on codeplex as well as a few others which gives a class in XNA to do just this using &lt;a href="http://directshownet.sourceforge.net/" target="_blank"&gt;DirectShow&lt;/a&gt;. Now all the examples I found seem to have originated from &lt;a href="http://www.codeproject.com/KB/game/VidTextureClassWebcamApp.aspx?fid=441427&amp;amp;df=90&amp;amp;mpp=25&amp;amp;noise=3&amp;amp;sort=Position&amp;amp;view=Quick&amp;amp;fr=26&amp;amp;select=2175249" target="_blank"&gt;this&lt;/a&gt; post on CodeProject, as does the class I am using here.&lt;/p&gt;
&lt;p&gt;Now you may be wondering, where is the movie clip for this....well fraps does not seem to play well with the method I am using to do this and so does not generate a nice clip for me to put up, wish it did because I think it&amp;#39;s pretty cool.&lt;/p&gt;
&lt;p&gt;Anyway, all I have done is got this lovely class&amp;#39;s output image on a 3D surface. This means you can have movies playing in your games in a virtual cinema, have adverts playing on an advertising billboard as you race past in your racer game, have a television like in GTA IV (I love the cartoons!)&lt;/p&gt;
&lt;p&gt;Clip of the Web Cam in XNA done by &lt;a href="http://www.youtube.com/user/MirageProject" target="_blank"&gt;MirageProject&lt;/a&gt; on YouTube, this gives you an idea of what my implementation looks like, but instead of giving it a device to read from you give it a *.wmv, *.mpg or any other supported format. Also, notice in my image above that the clip on the left is in black and white, this is down to the shader effect applied, not the clip.&lt;/p&gt;
&lt;p&gt;[Go to the site to view embedded media]&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can get the solution zip &lt;a class="" title="Source" href="http://xna-uk.net/files/folders/randomchaos/entry1973.aspx"&gt;here&lt;/a&gt;. (You have to supply your own clips or the zip would have been huge!!)&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=1962" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Source/default.aspx">Source</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+2.0/default.aspx">XNA 2.0</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Games/default.aspx">Games</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+Movie+Player/default.aspx">XNA Movie Player</category></item><item><title>RandomChaos Physics</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/05/20/randomchaos-physics.aspx</link><pubDate>Tue, 20 May 2008 13:21:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:1924</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=1924</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/05/20/randomchaos-physics.aspx#comments</comments><description>&lt;p&gt;[Go to the site to view embedded media]&lt;/p&gt;
&lt;p&gt;So I have started work on a simple physics engine. I intend to have it serve both 2D and 3D development. I guess it wont have all the lovely bits and bobs that engines like BulletX have, but what the hell, it&amp;#39;s going to help me understand how a Physics Engine works. Not sure about all the others, but this will naturally be exclusively for XNA and so written in C#. As ever I will put the full source up here once I have finished it so you can improve or add to it.&lt;/p&gt;
&lt;p&gt;So needless to say, other things may well be held up; Nova Wars and the Volumetric Cloud system being just two of them. As I think I have mentioned before; my goal now with the blog is to take all the samples I have put up and get them into an engine. This will also help me debug the samples as I will know what plays well with others and what does not. So I kind of think a Physics Engine, no matter how basic, will be a great addition.&lt;/p&gt;
&lt;p&gt;The clip in this post just shows where I currently am with the Physics Engine which is basic Force Generators. There is so much to do, not to mention adding 2D Physics too...I really have become an XNA junkie....help.....I don&amp;#39;t seem to be able to stop..&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=1924" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Collision+Detection/default.aspx">Collision Detection</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+2.0/default.aspx">XNA 2.0</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Physics/default.aspx">Physics</category></item><item><title>Generic XNA - Fire 2D &amp; 3D Shaders</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/05/19/generic-xna-fire-2d-amp-3d-shaders.aspx</link><pubDate>Mon, 19 May 2008 20:50:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:1922</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=1922</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/05/19/generic-xna-fire-2d-amp-3d-shaders.aspx#comments</comments><description>&lt;p&gt;[Go to the site to view embedded media]&lt;/p&gt;
&lt;p&gt;Well, this should be the final post on these flame shaders. As you have seen in the previous posts I have created 2D and 3D flame shaders based on the flame shader that comes with the NVIDIA SDK. When I initially created the 2D shader I found there was an issue with it when drawn with the 3D effect, turns out the issue was in the FireEmitterBase class, it was fixed by me setting the effect save state in the Begin call. Once that was done it all rendered fine.&lt;/p&gt;
&lt;p&gt;The only issue with the 2D flame shader is that you have to make a draw call per flame as an index.offset has to be passed per flame or the flame animations will all look the same.&lt;/p&gt;
&lt;p&gt;I have put them all in the same project, just added a new class for the 2D flame, as well as the shader it&amp;#39;s self and a floor class.&lt;/p&gt;
&lt;p&gt;Solution can be found &lt;a class="" title="source" href="http://xna-uk.net/files/folders/randomchaos/entry1923.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=1922" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Fire/default.aspx">Fire</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Source/default.aspx">Source</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+2.0/default.aspx">XNA 2.0</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/2D+Pixel+Shaders/default.aspx">2D Pixel Shaders</category></item><item><title>Generic XNA - Fire 2D</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/05/18/generic-xna-fire-2d.aspx</link><pubDate>Sun, 18 May 2008 21:42:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:1915</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=1915</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/05/18/generic-xna-fire-2d.aspx#comments</comments><description>&lt;p&gt;[Go to the site to view embedded media]&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Well, just posting my first attempt at getting the flame shader into 2D. It&amp;#39;s not perfect, but as it stands at the moment, will give you 2D flames. When played with the 3D flames it does not seem to render well, but on it&amp;#39;s own, as you can see it seems to be working. I am going to give it a bit of testing before I put the source out, but think it should be fine for pure 2D implementation, also still need to move the code into it&amp;#39;s own solution....&lt;/p&gt;
&lt;p&gt;As ever C&amp;amp;C welcome.&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=1915" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Fire/default.aspx">Fire</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+2.0/default.aspx">XNA 2.0</category></item><item><title>Generic XNA - 3D Fire Sample</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/05/09/generic-xna-3d-fire-sample.aspx</link><pubDate>Fri, 09 May 2008 19:42:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:1891</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=1891</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/05/09/generic-xna-3d-fire-sample.aspx#comments</comments><description>&lt;p&gt;Hope the music is acceptable this time...&lt;/p&gt;
&lt;p&gt;[Go to the site to view embedded media]&lt;/p&gt;
&lt;p&gt;Sorry about the quality of the clip, it was a 20MB clip, but I think youtube has crunched it up adn so it has lost some quality....&lt;/p&gt;
&lt;p&gt;OK, here is the code sample for the flame shader I have adapted from NVIDIA&amp;#39;s Volumetric Flame shader. The emitter class is pretty much the same as the billboard particle samples in my earlier posts, the real work is in the shader. I took the original NVIDIA flame shader and merged it with my billboard shader resulting in the sample you see here.&lt;/p&gt;
&lt;p&gt;Now the emitter given in this sample is very basic, but I am sure you will be able either derive from it or use it as a template for your own flame emitter.&lt;/p&gt;
&lt;p&gt;I have had a few requests regarding a 2D version of the shader so I am going to look at applying it to a 2D context.&lt;/p&gt;
&lt;p&gt;Solution can be located &lt;a class="" href="http://xna-uk.net/files/folders/randomchaos/entry1892.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=1891" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Fire/default.aspx">Fire</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Source/default.aspx">Source</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+2.0/default.aspx">XNA 2.0</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/HLSL/default.aspx">HLSL</category></item><item><title>SkyBox Shader Fix</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/05/09/skybox-shader-fix.aspx</link><pubDate>Fri, 09 May 2008 19:18:01 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:1889</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=1889</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/05/09/skybox-shader-fix.aspx#comments</comments><description>&lt;p&gt;I have found my skybox shader has a bug in it, the shader displays the cubemap as a reflection, the following shader code will give a better result.  &lt;p&gt;So, if you have used my skybox sample then replace the shader with this code:&lt;/p&gt; &lt;div style="border-right:black 1pt solid;border-top:black 1pt solid;overflow:auto;border-left:black 1pt solid;border-bottom:black 1pt solid;height:200pt;"&gt;&lt;pre class="code"&gt;Texture surfaceTexture;
samplerCUBE TextureSampler = sampler_state 
{ 
    texture = &amp;lt;surfaceTexture&amp;gt; ; 
    magfilter = LINEAR; 
    minfilter = LINEAR; 
    mipfilter = LINEAR; 
    AddressU = Mirror;
    AddressV = Mirror;
};

float4x4 World : World;
float4x4 View : View;
float4x4 Projection : Projection;

float3 EyePosition : CameraPosition;

struct VS_INPUT 
{
    float4 Position    : POSITION0;
    float3 Normal : NORMAL0;    
};

struct VS_OUTPUT 
{
    float4 Position    : POSITION0;
    float3 ViewDirection : TEXCOORD2;
        
};

float4 CubeMapLookup(float3 CubeTexcoord)
{    
    return texCUBE(TextureSampler, CubeTexcoord);
}

VS_OUTPUT Transform(VS_INPUT Input)
{
    float4x4 WorldViewProjection = mul(mul(World, View), Projection);
    float3 ObjectPosition = mul(Input.Position, World);
    
    VS_OUTPUT Output;
    Output.Position    = mul(Input.Position, WorldViewProjection);
    
    
    Output.ViewDirection = ObjectPosition - EyePosition;    
    
    return Output;
}

struct PS_INPUT 
{    
    float3 ViewDirection : TEXCOORD2;
};

float4 BasicShader(PS_INPUT Input) : COLOR0
{    
    float3 ViewDirection = normalize(Input.ViewDirection);    
    ViewDirection.x *= -1;
    return CubeMapLookup(ViewDirection);
}

technique BasicShader 
{
    pass P0
    {
        VertexShader = compile vs_2_0 Transform();
        PixelShader  = compile ps_2_0 BasicShader();
    }
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Sorry for giving you a crappy shader in the first place :P&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=1889" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/SkyBox/default.aspx">SkyBox</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+2.0/default.aspx">XNA 2.0</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/HLSL/default.aspx">HLSL</category></item><item><title>Generic XNA - Fire</title><link>http://xna-uk.net/blogs/randomchaos/archive/2008/05/07/generic-xna-fire.aspx</link><pubDate>Tue, 06 May 2008 23:10:00 GMT</pubDate><guid isPermaLink="false">cc259236-b685-459d-9e21-6c78c60b874c:1878</guid><dc:creator>Nemo Krad</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://xna-uk.net/blogs/randomchaos/rsscomments.aspx?PostID=1878</wfw:commentRss><comments>http://xna-uk.net/blogs/randomchaos/archive/2008/05/07/generic-xna-fire.aspx#comments</comments><description>&lt;p&gt;&lt;a class="" href="http://xna-uk.net/photos/screen_shots/images/1877/original.aspx"&gt;&lt;img height="200" alt="" hspace="150" src="http://xna-uk.net/photos/screen_shots/images/1877/original.aspx" width="300" border="0" /&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Yes, I have been playing with fire! I have had this flame shader by NVIDIA (from the SDK) for ages now. I tried to implement in my olf RC3D Engine but it was a poor attempt and was of no real use to anyone, but I think I may have the beginnings of a decent implementation now. I have mixed the NVIDIA shader based on Yury Uralsky&amp;#39;s &amp;quot;Volumetric Fire&amp;quot; with my billboard shader and billboard particle system and it seems to be playing quite well.&lt;/p&gt;
&lt;p&gt;Take a look at the clip and let me know what you think.&lt;/p&gt;
&lt;p&gt;[Go to the site to view embedded media]&lt;/p&gt;&lt;img src="http://xna-uk.net/aggbug.aspx?PostID=1878" width="1" height="1"&gt;</description><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Generic+XNA/default.aspx">Generic XNA</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/Fire/default.aspx">Fire</category><category domain="http://xna-uk.net/blogs/randomchaos/archive/tags/XNA+2.0/default.aspx">XNA 2.0</category></item></channel></rss>