<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blog.magenic.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Jeff Ferguson</title><subtitle type="html">proudly annoying fellow Magenic employees since 1996</subtitle><id>http://blog.magenic.com/blogs/jefff/atom.aspx</id><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blog.magenic.com/blogs/jefff/atom.aspx" /><generator uri="http://communityserver.org" version="2.1.60809.935">Community Server</generator><updated>2007-02-26T12:48:00Z</updated><entry><title>The Case For C++ In A .NET World</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2008/09/22/The-Case-For-C_2B002B00_-In-A-.NET-World.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2008/09/22/The-Case-For-C_2B002B00_-In-A-.NET-World.aspx</id><published>2008-09-23T02:14:00Z</published><updated>2008-09-23T02:14:00Z</updated><content type="html">&lt;p&gt;I am currently working with a client to bring .NET support to the &lt;a href="http://www.acresso.com/products/installation/flexnet_publisher.htm"&gt;FLEXnet Publisher&lt;/a&gt; Licensing Toolkit version 11.6. The toolkit, as shipped, is very tightly tied to the C language, and it is designed for use with client applications written in C. My client, however, wants to use the toolkit from VB.NET 2.0 applications. I set out, therefore, with a vision that stated &amp;quot;make FLEXnet look like .NET&amp;quot;. &lt;/p&gt;&lt;p&gt;My first approach to the problem was to use P/Invoke to write a wrapper in VB.NET 2.0. This wrapper would sit just above the FLEXnet Publisher API and would wrap the function calls within a set of .NET classes that would hide the complexities of the API. With this architecture, .NET client applications would reference the .NET 2.0 wrapper, and the client calls to the wrapper would delegate the client&amp;rsquo;s method and property calls down to one or more calls to the FLEXnet Publisher API. &lt;/p&gt;&lt;p&gt;This initial effort was moderately, but not completely, successful. Some of the main problems with this approach had to do with the FLEXnet Publisher API&amp;rsquo;s design which mandated: &lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Explicit Pointer Casting&lt;/strong&gt;: Calls to lc_get_attr() and lc_set_attr() require, in some cases, that the pointer used to reference the value (or its placeholder) be cast to a pointer of a specific type, which is sometimes further obscured by a typedef, such as LM_A_VAL_TYPE or LM_SHORT_PTR. Pointer casts to different target types in a single VB.NET P/Invoke scenario are problematic. &lt;/li&gt;&lt;li&gt;&lt;strong&gt;Macro-Based Variable Declaration and Initialization&lt;/strong&gt;: One of the datatypes used in the call to lc_cryptstr() must be declared by a C macro called LM_CODE() and must be initialized by a C macro called LM_CODE_GEN_INIT(). These macros are found in C header files that ship with the FLEXnet Publisher API and their translation into a VB.NET environment for use with a .NET layer proved to be very difficult.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Given the discovery of the FLEXnet Publisher API&amp;rsquo;s tight coupling to C, I devised a new approach. The approach shared the vision of the first approach &amp;ndash; namely, &amp;quot;to use a .NET wrapper to wrap the FLEXnet API calls into a .NET environment&amp;quot; &amp;ndash; but was built using both native (unmanaged) C++ and C++/CLI (formerly known as Managed C++). The new approach involved two steps: &lt;/p&gt;&lt;ol&gt;&lt;li&gt;Wrap the FLEXnet API calls into a native, unmanaged, C++ class. The methods exposed by this class would be implemented via calls into the FLEXnet API. &lt;/li&gt;&lt;li&gt;Build a C++/CLI class that delegates its calls to an obect of the native, unmanaged C++ class.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;The main advantage to this approach is that the native C++ class can make use of all of the C header files, data types, libraries and macros that ship with the FLEXnet API, thereby providing a level of programmability not possible with a 100% .NET language solution and P/Invoke. The disadvantage to this approach is one of maintenance, as it requires someone with knowledge of C++. However, the vision statement of &amp;ldquo;make FLEXnet look like .NET&amp;rdquo; can best be realized using C++, and I am a firm believer in using the right tool for the right job. &lt;/p&gt;&lt;p&gt;This approach proved to be much more successful, and, thanks to Visual C++&amp;rsquo;s ability to include unmanaged and managed code in the same assembly &amp;ndash; a feature unavailable to other .NET languages &amp;ndash; both the native C++ class and the C++/CLI class can be included in a single assembly. The assembly exposes the C++/CLI class as a standard .NET class which can be referenced from the client&amp;rsquo;s VB.NET applications. Problem solved. &lt;/p&gt;&lt;p&gt;Here is an example of that approach. In this example, we will wrap the FLEXnet API lc_cryptstr() with a .NET class that can be used by .NET applications. The unmanaged, native C++ class, which is the class that will call the FLEXnet API, is declared in a header file as follows: &lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;class UnmanagedWrapper&lt;br /&gt;{&lt;br /&gt;public:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; UnmanagedWrapper(void);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static int EncryptLicenseString(char * InputString, char ** OutputString);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ~UnmanagedWrapper(void);&lt;br /&gt;};&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The implementation of the class performs the work that needs to take place to wrap the FLEXnet API call: &lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;UnmanagedWrapper::UnmanagedWrapper(void)&lt;br /&gt;{&lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;int UnmanagedWrapper::EncryptLicenseString(char * InputString, char ** OutputString)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LM_HANDLE * lm_job;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; char * errors; &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LM_CODE(code, ENCRYPTION_SEED1, ENCRYPTION_SEED2, VENDOR_KEY1, VENDOR_KEY2, VENDOR_KEY3, VENDOR_KEY4, VENDOR_KEY5);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LM_CODE_GEN_INIT(&amp;amp;code);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lc_init(NULL, VENDOR_NAME, &amp;amp;code, &amp;amp;lm_job);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return lc_cryptstr(lm_job, InputString, OutputString, &amp;amp;code, LM_CRYPT_FORCE, NULL, &amp;amp;errors);&lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;UnmanagedWrapper::~UnmanagedWrapper(void)&lt;br /&gt;{&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Now that the native C++ class is built, the C++/CLI class can be built. Its job is to simply hold a pointer to the native C++ class and delegate calls to the native C++ class: &lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;public ref class OwnerLicensingEngine&lt;br /&gt;{&lt;br /&gt;private:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; UnmanagedWrapper * thisUnmanagedWrapper; &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;public:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; OwnerLicensingEngine()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; thisUnmanagedWrapper = new UnmanagedWrapper();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String^ EncryptLicenseString(String^ Input)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IntPtr InputAsIntPtr = Marshal::StringToHGlobalAnsi(Input);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; char * InputAsString = (char *)(void *)InputAsIntPtr;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; char * OutputString; &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; thisUnmanagedWrapper-&amp;gt;EncryptLicenseString(InputAsString, &amp;amp;OutputString);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String ^ StringToReturn = gcnew String(OutputString);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Marshal::FreeHGlobal(InputAsIntPtr);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return StringToReturn;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ~OwnerLicensingEngine()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; delete thisUnmanagedWrapper;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;};&lt;/font&gt; &lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The private variable thisUnmanagedWrapper points to an object of the native C++ class. The object is created in the C++/CLI class constructor and destroyed in the C++/CLI class destructor. The C++/CLI&amp;rsquo;s method implementation translates data types as necessary (in the example above, .NET String objects need to be available as character pointers usable by the native C++ class), calls the appropriate native C++ method, performs any necessary cleanup, and returns. At this point, the C++/CLI class is ready to go and can be used by .NET clients, such as in this unit test: &lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;lt;TestMethod()&amp;gt; Public Sub TestEncryptLicenseString()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim TestFLEXnetObject As New OwnerLicensingEngine&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim Input As String&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim Signed As String &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Input = &amp;quot;FEATURE f1 JEFF 1.000 permanent uncounted HOSTID=0003ffb91c18 SIGN=&amp;quot;&amp;quot;0&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Signed = Nothing&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Signed = TestFLEXnetObject.EncryptLicenseString(Input)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.IsNotNull(Signed)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.AreNotEqual(Input, Signed)&lt;br /&gt;End Sub&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;It took me a while to figure out all of the compiler and linker options to get a successful build, given the mix of .NET, FLEXnet libraries, and the Visual C++ runtime. The command line for the debug builds are shown below. &lt;/p&gt;&lt;p&gt;The compiler command line for the debug builds is as follows: &lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;/Od&lt;br /&gt;/I &amp;quot;[path to FLEXnet install]\machind&amp;quot;&lt;br /&gt;/D &amp;quot;WIN32&amp;quot;&lt;br /&gt;/D &amp;quot;_DEBUG&amp;quot;&lt;br /&gt;/D &amp;quot;_WINDLL&amp;quot;&lt;br /&gt;/D &amp;quot;_UNICODE&amp;quot;&lt;br /&gt;/D &amp;quot;UNICODE&amp;quot;&lt;br /&gt;/FD&lt;br /&gt;/EHa&lt;br /&gt;/MDd&lt;br /&gt;/Yu&amp;quot;stdafx.h&amp;quot;&lt;br /&gt;/Fp&amp;quot;Debug\ClientLicensingEngine.pch&amp;quot;&lt;br /&gt;/Fo&amp;quot;Debug\\&amp;quot;&lt;br /&gt;/Fd&amp;quot;Debug\vc90.pdb&amp;quot;&lt;br /&gt;/W3&lt;br /&gt;/nologo&lt;br /&gt;/c&lt;br /&gt;/Zi&lt;br /&gt;/clr&lt;br /&gt;/TP&lt;br /&gt;/errorReport:prompt&lt;br /&gt;/FU &amp;quot;c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll&amp;quot;&lt;br /&gt;/FU &amp;quot;c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll&amp;quot;&lt;br /&gt;/FU &amp;quot;c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.XML.dll&amp;quot;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The linker command line is as follows:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;/OUT:&amp;quot;C:\Projects\&amp;hellip;\ClientLicensingEngine.dll&amp;quot;&lt;br /&gt;/INCREMENTAL&lt;br /&gt;/NOLOGO&lt;br /&gt;/LIBPATH:&amp;quot;[path to FLEXnet install]\i86_n3&amp;quot;&lt;br /&gt;/DLL&lt;br /&gt;/MANIFEST&lt;br /&gt;/MANIFESTFILE:&amp;quot;Debug\ClientLicensingEngine.dll.intermediate.manifest&amp;quot;&lt;br /&gt;/MANIFESTUAC:&amp;quot;level=&amp;#39;asInvoker&amp;#39; uiAccess=&amp;#39;false&amp;#39;&amp;quot;&lt;br /&gt;/NODEFAULTLIB&lt;br /&gt;/DEBUG&lt;br /&gt;/ASSEMBLYDEBUG&lt;br /&gt;/PDB:&amp;quot;c:\Projects\&amp;hellip;\ClientLicensingEngine.pdb&amp;quot;&lt;br /&gt;/DYNAMICBASE&lt;br /&gt;/FIXED:No&lt;br /&gt;/NXCOMPAT&lt;br /&gt;/MACHINE:X86&lt;br /&gt;/ERRORREPORT:PROMPT&lt;br /&gt;lm_new_md.obj&lt;br /&gt;lmgr_md.lib&lt;br /&gt;libcrvs_md.lib&lt;br /&gt;libsb_md.lib&lt;br /&gt;[path to FLEXnet install]\i86_n3\activation\lib\libnoact_md.lib&lt;br /&gt;msvcrtd.lib&lt;br /&gt;msvcmrtd.lib&lt;br /&gt;mscoree.lib&lt;br /&gt;oldnames.lib&lt;br /&gt;kernel32.lib&lt;br /&gt;user32.lib&lt;br /&gt;netapi32.lib&lt;br /&gt;advapi32.lib&lt;br /&gt;gdi32.lib&lt;br /&gt;comdlg32.lib&lt;br /&gt;comctl32.lib&lt;br /&gt;wsock32.lib&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The compiler and linker options shown above will need to be adjusted for release builds so that the release-mode libraries are included. &lt;/p&gt;&lt;p&gt;If you find this solution to be elegant, don&amp;rsquo;t thank me. Thank Visual C++, which combines its support for native C with its support for .NET to provide a simple language solution to difficult problems whose problem domains are firmly rooted in a C-based API. I worked in C and C++ for many years before switching to C&lt;sup&gt;#&lt;/sup&gt; and VB.NET in the summer of 2000. I didn&amp;rsquo;t look back at C++, until now, but, given the problem I faced with this project, I am grateful for its continued presence in the Visual Studio family of languages. &lt;/p&gt;&lt;p&gt;Use the right tool for the right job. C++ still has its place.&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=6352" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="C++/CLI" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/C_2B002B002F00_CLI/default.aspx" /></entry><entry><title>Magenic Technology Summit</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2008/05/02/Magenic-Technology-Summit.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2008/05/02/Magenic-Technology-Summit.aspx</id><published>2008-05-02T14:11:00Z</published><updated>2008-05-02T14:11:00Z</updated><content type="html">&lt;p&gt;The first ever &lt;strong&gt;Magenic Technology Summit&lt;/strong&gt; is being held on Fri Jun&amp;nbsp;20 2008&amp;nbsp;in Downers Grove near Chicago!&lt;/p&gt;&lt;p&gt;This is a full day of Magenic-provided training, available by invitation only to customers and potential customers of Magenic. We have lined up an impressive array of speakers and topics in two tracks, .NET development and Microsoft servers. And we have lined up two keynotes.&lt;/p&gt;&lt;p&gt;Our first keynote speaker is Jay Schmelzer, who is the Group Program Manager for RAD tools. This basically means he runs the teams for all the Visual Studio designers and related RAD tools. He&amp;rsquo;s an excellent good speaker and should provide some great insight into the present and future of RAD development tools from Microsoft.&lt;/p&gt;&lt;p&gt;Our second keynote speaker is Rockford Lhotka, Magenic&amp;rsquo;s Technology Evangelist and the creator of the very popular CSLA .NET development framework.&lt;/p&gt;&lt;p&gt;But more importantly, two tracks of in-depth technical content straight from the experts at Magenic. Topics covering the present and future of .NET development and key Microsoft server products. An event like this doesn&amp;rsquo;t come along every day, and shouldn&amp;rsquo;t be missed!&lt;/p&gt;&lt;p&gt;The event is FREE to our customers (though you will need to cover any travel costs), and includes lunch and a reception at the end of the day. We hope to see you there!&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;Development Track Sessions&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Writing Better Code&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;It&amp;rsquo;s one thing to write code that will do the job in the short-term; it&amp;rsquo;s another challenge to write code that can stand the test of time. In this session, Jason Bock will show you how you can use tools and features in VS 2008 (e.g. unit testing, CodeAnalysis, Code Coverage, etc.) to assist you in making your code maintainable over time, easy to understand, and resilient to defects.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test Driven Development with ASP.NET MVC Framework - Building better AJAX web sites!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;In this session, Nermin Dibek will disprove two myths: 1) It is nearly impossible to build a Test Driven Web in ASP.NET, and 2) Building custom Ajax implementations is too hard in ASP.NET.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Super-Optimized Microsoft LINQ: Indexed Objects&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;In this session, Aaron Erickson will go into how developers can use indexing techniques to allow for &amp;ldquo;constant-time&amp;rdquo; queries using LINQ to Objects. The session will cover both case studies of where this technique is used in technologies such as the i4o open source project and CSLA, as well as examples of how developers can implement the technique in their own custom collections.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;User Experience and Better Designer Developer Collaboration&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Anthony Handley will discuss the importance of good user experience design and how tool and technologies like WPF, Silverlight and Expression Blend are helping designers collaborate better with developers.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;Server Track Sessions&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Microsoft Office SharePoint Server Branding&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;The first thing everyone wants to do to their SharePoint installation is make it not look like SharePoint. There are many levels to SharePoint branding and the deeper you go, the more work it is. In this session Michael Cummings will cover the various ways of branding SharePoint from the &amp;ldquo;I don&amp;rsquo;t know HTML&amp;rdquo; level to the &amp;ldquo;I eat C# code for breakfast&amp;rdquo; level and also show you some areas to avoid when branding and better methods of accomplishing your goal.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Dynamic Management Views Will Save Your Life&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Do you ever wish you could get an X-Ray view of your sluggish SQL Server? Take the guesswork out of troubleshooting database issues by utilizing the data collected in Dynamic Management Views. In this session, Whitney Weaver will show you the tools implemented in SQL Server 2005 that give you extraordinary view into the health and well being of your database instance. Code samples will be presented around query performance, I/O utilization, index usage, and more.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Really&amp;hellip;what is Team Foundation Server?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Team Foundation Server (TFS) has been out on the market for a couple years and there is still more confusion about it, what it does and who can benefit from it. In this session Scott Wylie will show how each member of your team, project and company can use and benefit from TFS and how it can make life much simpler when it comes to all application lifecycle tasks.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;RFID: The future is now&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;In the movie Minority Report, we were shown a glimpse of what the future might bring. Well that future is now. With the release of BizTalk 2006 R2, we were given a powerful new tool and technology. In this session, Shawn Doty will give an overview of what RFID is, how to implement it, and how it is used.&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=3938" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author></entry><entry><title>Twin Cities Code Camp Session Slides and Code Posted</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2008/04/07/Twin-Cities-Code-Camp-Session-Slides-and-Code-Posted.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2008/04/07/Twin-Cities-Code-Camp-Session-Slides-and-Code-Posted.aspx</id><published>2008-04-07T16:39:00Z</published><updated>2008-04-07T16:39:00Z</updated><content type="html">&lt;p&gt;I presented a talk entitled &lt;em&gt;&lt;a href="http://www.twincitiescodecamp.com/TCCC/Spring2008/Sessions.aspx#s7"&gt;A Tour of the Parallel Extensions to the .NET Framework&lt;/a&gt;&lt;/em&gt; last Sat Apr 05 2008 at the &lt;a href="http://www.twincitiescodecamp.com/TCCC/Default.aspx"&gt;Twin Cities Code Camp&lt;/a&gt;. The talk went well and I presented for about 70 minutes. Thanks to all who attended. My slide deck and code samples from the talk are available &lt;a href="http://cid-7062094a08532907.skydrive.live.com/browse.aspx/Twin%20Cities%20Code%20Camp%20IV"&gt;here&lt;/a&gt;. Feel free to grab the slides and code, and feel free to come back to me with comments or questions (like most slide decks and code samples, they may not make too much sense in isolation and may require &amp;quot;speaker input&amp;quot;).&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=3008" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="Twin Cities Code Camp" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Twin+Cities+Code+Camp/default.aspx" /><category term="Parallel Extensions to the .NET Framework" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Parallel+Extensions+to+the+.NET+Framework/default.aspx" /></entry><entry><title>Another Case for the Declarative Programming Model</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2008/04/03/Another-Case-for-the-Declarative-Programming-Model.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2008/04/03/Another-Case-for-the-Declarative-Programming-Model.aspx</id><published>2008-04-03T19:55:00Z</published><updated>2008-04-03T19:55:00Z</updated><content type="html">&lt;p&gt;&lt;a href="http://blogs.msdn.com/toub/"&gt;Stephen Toub&lt;/a&gt; has written a &lt;a href="http://blogs.msdn.com/pfxteam/archive/2008/04/03/8354128.aspx"&gt;blog post&lt;/a&gt; on the &lt;a href="http://blogs.msdn.com/pfxteam/"&gt;Parallel Programming with .NET&lt;/a&gt; blog reflecting his experiences in presenting the Parallel Extensions to conference attendees. The first question he fielded, and the one I run across quite often, is this:&lt;/p&gt;&lt;p&gt;&amp;quot;&lt;em&gt;This stuff looks really cool, but why do we need to modify our code to use Parallel Extensions; why can&amp;#39;t you just automatically parallelize it for me?&lt;/em&gt;&amp;quot;&lt;/p&gt;&lt;p&gt;The answer, in part, is that our current imperative style of programming makes it difficult for code analysis engines to figure out what a block of code is really trying to do, thereby hampering efforts by the engine to figure out what code should be run in parallel. Doing a simple replacement of &lt;font face="courier new,courier"&gt;for&lt;/font&gt; with &lt;font face="courier new,courier"&gt;Parallel.For&lt;/font&gt; is not a good solution, because the sequential for loops might be modifying state that, when run in parallel, will cause unpredictable results, especially when working with data structures that contain thread-unsafe code (methods like &lt;font face="courier new,courier"&gt;List&amp;lt;&amp;gt;.Add()&lt;/font&gt; are not thread-safe).&lt;/p&gt;&lt;p&gt;Code analysis engines can do a better job of injecting optimizations such as parallelism if we write code to say what we want done (which is the declarative style of programming) rather than how we want it done (which is the imperative style of programming). If we write code that says &amp;quot;give me these results&amp;quot; rather than &amp;quot;execute this loop and modify these data structures&amp;quot;, code execution engines can do a better job of optimizing executing code that is sensitive to a machine&amp;#39;s hardware capabilities.&lt;/p&gt;&lt;p&gt;When LINQ was first introduced, I failed to see its benefits. After all, I thought, it was simply syntactic sugar over &lt;font face="courier new,courier"&gt;foreach&lt;/font&gt;, which is itself syntactic sugar over &lt;font face="courier new,courier"&gt;IEnumerable&lt;/font&gt; and friends (&amp;quot;Syntactic Sugar&amp;quot; would be a great band name, by the way). After reflecting on LINQ, I have recently found it to be significant not on its own merits so much as what it points to: a declarative style of programming that says &amp;quot;perform this action&amp;quot; instead of &amp;quot;run this code&amp;quot;.&lt;/p&gt;&lt;p&gt;This model has gotten a lot of press lately, through technologies like LINQ and F&lt;sup&gt;#&lt;/sup&gt;. However, this declarative style is nothing new. You have most likely been working&amp;nbsp;in a declarative model for years, if not decades, and just haven&amp;#39;t realized it. It&amp;#39;s called SQL.&lt;/p&gt;&lt;p&gt;Here&amp;#39;s what you write in SQL:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;SELECT * FROM Customers WHERE State = &amp;#39;MN&amp;#39;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Here&amp;#39;s what you don&amp;#39;t write in SQL:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;Row [] MinnesotaRows;&lt;br /&gt;&lt;/font&gt;&lt;font face="courier new,courier"&gt;Table CustomersTable = new Table(&amp;quot;Customers&amp;quot;);&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;font face="courier new,courier"&gt;if (CustomersTable.HasIndices == true)&lt;br /&gt;&lt;/font&gt;&lt;font face="courier new,courier"&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Index StateIndex = CustomersTable.GetIndex(&amp;quot;State&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MinnesotaRows = StateIndex.FindRows(&amp;quot;MN&amp;quot;);&lt;/font&gt;&lt;font face="courier new,courier"&gt;&lt;br /&gt;&lt;/font&gt;&lt;font face="courier new,courier"&gt;}&lt;br /&gt;&lt;/font&gt;&lt;font face="courier new,courier"&gt;else&lt;br /&gt;&lt;/font&gt;&lt;font face="courier new,courier"&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; List&amp;lt;Row&amp;gt; RowList = new List&amp;lt;Row&amp;gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach(Row CurrentRow in CustomersTable.Rows)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(CurrentRow.Column[&amp;quot;State&amp;quot;].Equals(&amp;quot;MN&amp;quot;) == true)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RowList.Add(CurrentRow);&lt;/font&gt;&lt;font face="courier new,courier"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MinnesotaRows = RowList.ToArray();&lt;/font&gt;&lt;font face="courier new,courier"&gt;&lt;br /&gt;&lt;/font&gt;&lt;font face="courier new,courier"&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The first example is &amp;quot;go get this result set&amp;quot; and the second example is &amp;quot;run this code recipe&amp;quot;. The first example abstracts the &amp;quot;what&amp;quot; from the &amp;quot;how&amp;quot; and allows the SQL engine to use whatever algorithms best suited to satisfy the results. Parallelism is just one example of an algorithm that the SQL engine may or may not employ; the original declarative-style query statement&amp;nbsp;will survive any algorithm change within the engine.&lt;/p&gt;&lt;p&gt;I ask you to consider the declarative programming style in that light. It&amp;#39;s not just some &amp;quot;new way of doing things&amp;quot; - it&amp;#39;s a model that let&amp;#39;s us developers shift focus away from somewhat-brittle algorithmic details and focusing instead on writing code that describes what we need done. With a model like that, we&amp;#39;ll move closer to allowing code analysis tools to automatically parallelizing code, because we won&amp;#39;t have to write algorithms that either depend upon, or exclude us from, parallelism.&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=2910" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="Parallel Extensions to the .NET Framework" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Parallel+Extensions+to+the+.NET+Framework/default.aspx" /></entry><entry><title>Parallel For Loops with Thread Local State</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2008/03/31/Parallel-For-Loops-with-Thread-Local-State.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2008/03/31/Parallel-For-Loops-with-Thread-Local-State.aspx</id><published>2008-03-31T23:14:00Z</published><updated>2008-03-31T23:14:00Z</updated><content type="html">&lt;p&gt;A &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3084287&amp;amp;SiteID=1"&gt;recent MSDN&amp;nbsp;post&lt;/a&gt; contained the following code:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;string[] entropy_array = { &amp;quot;e&amp;quot;, &amp;quot;r&amp;quot;, &amp;quot;t&amp;quot;, &amp;quot;y&amp;quot;, &amp;quot;u&amp;quot;, &amp;quot;ı&amp;quot;, &amp;quot;o&amp;quot;, &amp;quot;p&amp;quot;, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;quot;ğ&amp;quot;, &amp;quot;&amp;uuml;&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;s&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;f&amp;quot;, &amp;quot;g&amp;quot;, &amp;quot;h&amp;quot;, &amp;quot;j&amp;quot;, &amp;quot;k&amp;quot;, &amp;quot;l&amp;quot;,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;quot;ş&amp;quot;, &amp;quot;i&amp;quot;, &amp;quot;z&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;v&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;n&amp;quot;, &amp;quot;m&amp;quot;, &amp;quot;&amp;ccedil;&amp;quot;, &amp;quot;&amp;ouml;&amp;quot;, &amp;quot; &amp;quot;};&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;Dictionary&amp;lt;string, int&amp;gt; entropy = new Dictionary&amp;lt;string, int&amp;gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;Parallel.For(0, entropy_array.Length, i =&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int j = 0; j &amp;lt; entropy_array.Length; j++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; entropy.Add(entropy_array[i] + entropy_array[j], 0);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;});&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The author of the post, who&amp;nbsp;wanted to&amp;nbsp;add all two letter combinations from the letters found in the&amp;nbsp;&lt;font face="courier new,courier"&gt;entropy_array&lt;/font&gt; array into the&amp;nbsp;&lt;font face="courier new,courier"&gt;entropy&lt;/font&gt; dictionary,&amp;nbsp;wondered why the number of elements in the dictionary varied from run to run.&lt;/p&gt;&lt;p&gt;The problem with the code is that the introduction of &lt;font face="courier new,courier"&gt;Parallel.For()&lt;/font&gt; brings multiple threads into the picture, and, since &lt;font face="courier new,courier"&gt;Dictionary&amp;lt;T1, T2&amp;gt;.Add()&lt;/font&gt; isn&amp;#39;t thread safe, correctness can&amp;#39;t be guaranteed.&lt;/p&gt;&lt;p&gt;Huseyin Yildiz proposed a solution to this problem. His solution involved setting up a dictionary for each thread created by &lt;font face="courier new,courier"&gt;Parallel.For()&lt;/font&gt;, letting each thread do its work, and placing the intermediate results in the thread-local dictionary. Once the thread ended, the results from the thread-local dictionary could be added to the main dictionary.&lt;/p&gt;&lt;p&gt;This scenario is entirely possible using one of the overloads to &lt;font face="courier new,courier"&gt;Parallel.For()&lt;/font&gt;. I&amp;#39;ll start by posting the solution, built as a simple console application, and will spend the rest of this blog post explaining the code:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;class Program&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static void Main(string[] args)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dictionary&amp;lt;string, int&amp;gt; Entropy = new Dictionary&amp;lt;string, int&amp;gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string[] EntropyArray = { &amp;quot;e&amp;quot;, &amp;quot;r&amp;quot;, &amp;quot;t&amp;quot;, &amp;quot;y&amp;quot;, &amp;quot;u&amp;quot;, &amp;quot;ı&amp;quot;, &amp;quot;o&amp;quot;, &amp;quot;p&amp;quot;, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;quot;ğ&amp;quot;, &amp;quot;&amp;uuml;&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;s&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;f&amp;quot;, &amp;quot;g&amp;quot;, &amp;quot;h&amp;quot;, &amp;quot;j&amp;quot;, &amp;quot;k&amp;quot;, &amp;quot;l&amp;quot;,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;quot;ş&amp;quot;, &amp;quot;i&amp;quot;, &amp;quot;z&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;v&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;n&amp;quot;, &amp;quot;m&amp;quot;, &amp;quot;&amp;ccedil;&amp;quot;, &amp;quot;&amp;ouml;&amp;quot;, &amp;quot; &amp;quot;};&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Parallel.For(&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntropyArray.Length,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; () =&amp;gt; new Dictionary&amp;lt;string, int&amp;gt;(),&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (i, state) =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int j = 0; j &amp;lt; EntropyArray.Length; j++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; state.ThreadLocalState.Add(EntropyArray[i] + EntropyArray[j], 0);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; },&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (SubList) =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lock (EntropyArray)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (KeyValuePair&amp;lt;string, int&amp;gt; Pair in SubList)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Entropy.Add(Pair.Key, Pair.Value);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The overload of &lt;font face="courier new,courier"&gt;Parallel.For()&lt;/font&gt; employed here takes five parameters:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;&lt;font face="courier new,courier"&gt;int fromExclusive&lt;/font&gt;&lt;/strong&gt;: The zero-based index used as the start of the loop.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;font face="courier new,courier"&gt;int toExclusive&lt;/font&gt;&lt;/strong&gt;: The zero-based index used as the stopping point of the loop. Once the loop indexer gets to this value, processing wil stop.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;font face="courier new,courier"&gt;Func&amp;lt;Tlocal&amp;gt; threadLocalSelector&lt;/font&gt;&lt;/strong&gt;: A function that returns the object to be used as the thread-local object for the thread executing its piece of the total loop.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;font face="courier new,courier"&gt;Action&amp;lt;int,ParallelState&amp;lt;Tlocal&amp;gt;&amp;gt; body&lt;/font&gt;&lt;/strong&gt;: The action to be taken by each loop iteration.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;font face="courier new,courier"&gt;Action&amp;lt;Tlocal&amp;gt; threadLocalCleanup&lt;/font&gt;&lt;/strong&gt;: A function specifying cleanup code to be executed once the thread&amp;#39;s portion of the processing has finished.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The &lt;font face="courier new,courier"&gt;fromExclusive&lt;/font&gt; parameter in my example is simple:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0,&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The &lt;font face="courier new,courier"&gt;toExclusive&lt;/font&gt; parameter in my example is also simple:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntropyArray.Length,&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The &lt;font face="courier new,courier"&gt;threadLocalSelector&lt;/font&gt; parameter is a function body that creates a new string-to-int dictionary to be used as the thread-local object for the thread:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; () =&amp;gt; new Dictionary&amp;lt;string, int&amp;gt;(),&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The &lt;font face="courier new,courier"&gt;body&lt;/font&gt; parameter performs the processing, placing the results in the thread-local dictionary:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (i, state) =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int j = 0; j &amp;lt; EntropyArray.Length; j++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; state.ThreadLocalState.Add(EntropyArray[i] + EntropyArray[j], 0);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; },&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The&amp;nbsp;first parameter passed to the body is the loop indexer, and the second parameter is an object of type &lt;font face="courier new,courier"&gt;ParallelState&lt;/font&gt;, which has a property called &lt;font face="courier new,courier"&gt;ThreadLocalState&lt;/font&gt; containing the thread-local object.&lt;/p&gt;&lt;p&gt;Finally, the &lt;font face="courier new,courier"&gt;threadLocalCleanup&lt;/font&gt; parameter specifies the code run when the thread has finished its execution:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (SubList) =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lock (EntropyArray)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (KeyValuePair&amp;lt;string, int&amp;gt; Pair in SubList)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Entropy.Add(Pair.Key, Pair.Value);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;This code, which is called with the thread-local object as a parameter, adds the items in the thread-local list to the main list. Because the &lt;font face="courier new,courier"&gt;Add()&lt;/font&gt; method is not thread safe, the code block is locked.&lt;/p&gt;&lt;p&gt;The caveat to all of this is that this code, with its copying and object allocations, may not be as performant as a sequential algorithm, given the small input set. Given a larger input set, however, the performance gains will most likely outweigh the additional overhead.&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=2841" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="Parallel Extensions to the .NET Framework" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Parallel+Extensions+to+the+.NET+Framework/default.aspx" /></entry><entry><title>Parallel.ForEach Sample: Fibonacci Numbers</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2008/03/18/Parallel.ForEach-Sample_3A00_-Fibonacci-Numbers.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2008/03/18/Parallel.ForEach-Sample_3A00_-Fibonacci-Numbers.aspx</id><published>2008-03-18T15:45:00Z</published><updated>2008-03-18T15:45:00Z</updated><content type="html">&lt;p&gt;A &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3022013&amp;amp;SiteID=1"&gt;post on the MSDN Forums&lt;/a&gt; recently asked for a sample illustrating the use of &lt;font face="courier new,courier"&gt;Parallel.ForEach()&lt;/font&gt;, so I decided to build up a small Fibonacci number calculation sample over my lunch hour. Here it is:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;class Program&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //---------------------------------------------------------------------&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //---------------------------------------------------------------------&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static void Main(string[] args)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //-----------------------------------------------------------------&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Create a new list and add random integers to it.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //-----------------------------------------------------------------&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; List&amp;lt;int&amp;gt; Integers = new List&amp;lt;int&amp;gt;(20);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Random RandomIntGenerator = new Random(DateTime.Now.Millisecond);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i =&amp;nbsp;0; i &amp;lt; Integers.Capacity; i++)&lt;/font&gt;&lt;font face="courier new,courier"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Integers.Add(RandomIntGenerator.Next(1, 20));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //-----------------------------------------------------------------&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Iterate over the list in a parallel fashion and calculate&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Fibonacci numbers for each of the list items.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //-----------------------------------------------------------------&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Parallel.ForEach&amp;lt;int&amp;gt;(Integers, (i) =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&amp;quot;[{0}] F({1}) = {2}&amp;quot;, Thread.CurrentThread.Name, i, CalculateFibonacciNumber(i));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //---------------------------------------------------------------------&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //---------------------------------------------------------------------&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static int CalculateFibonacciNumber(int Value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Thread.Sleep(1);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (Value &amp;lt; 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new ArgumentOutOfRangeException(&amp;quot;CalculateFibonacciNumber&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (Value == 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (Value == 1)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int Fibonacci = CalculateFibonacciNumber(Value - 1) + CalculateFibonacciNumber(Value - 2);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return Fibonacci;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The output (which will differ every time due to the randomness built into the sample) will look something like this:&amp;nbsp;&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;[Worker 2] F(1) = 1&lt;br /&gt;[Worker 2] F(10) = 55&lt;br /&gt;[Worker 2] F(2) = 1&lt;br /&gt;[Worker 2] F(11) = 89&lt;br /&gt;[Worker 3] F(13) = 233&lt;br /&gt;[Worker 3] F(10) = 55&lt;br /&gt;[Worker 3] F(1) = 1&lt;br /&gt;[Worker 1] F(14) = 377&lt;br /&gt;[Worker 2] F(15) = 610&lt;br /&gt;[Worker 3] F(15) = 610&lt;br /&gt;[Worker 2] F(13) = 233&lt;br /&gt;[Worker 2] F(9) = 34&lt;br /&gt;[Worker 2] F(5) = 5&lt;br /&gt;[Worker 1] F(17) = 1597&lt;br /&gt;[Worker 1] F(9) = 34&lt;br /&gt;[Worker 1] F(13) = 233&lt;br /&gt;[Worker 1] F(13) = 233&lt;br /&gt;[Worker 1] F(10) = 55&lt;br /&gt;[Worker 1] F(11) = 89&lt;br /&gt;[Worker 1] F(7) = 13&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Originally, I loaded the&amp;nbsp;input list&amp;nbsp;using&amp;nbsp;parallel code:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="Courier New"&gt;//-----------------------------------------------------------------&lt;br /&gt;// Create a new list and add random integers to it.&lt;br /&gt;//-----------------------------------------------------------------&lt;br /&gt;List&amp;lt;int&amp;gt; Integers = new List&amp;lt;int&amp;gt;(20);&lt;br /&gt;Random RandomIntGenerator = new Random(DateTime.Now.Millisecond);&lt;br /&gt;Parallel.For(0, Integers.Capacity, (i) =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Integers.Add(RandomIntGenerator.Next(1, 20));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;However, &lt;a href="http://blogs.msdn.com/toub/"&gt;Stephen Toub&lt;/a&gt; noted three issues with this approach:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The &lt;font face="courier new,courier"&gt;Random&lt;/font&gt; class is not thread-safe and could be misused when multiple threads are involved.&lt;/li&gt;&lt;li&gt;The &lt;font face="courier new,courier"&gt;List&amp;lt;&amp;gt;.Add()&lt;/font&gt; method is not thread-safe and could be misused when multiple threads are involved.&lt;/li&gt;&lt;li&gt;So little work is being done in the loop that the overhead of the parallelization infrastructure may actually cause the code to be less performant than a sequential loop.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Other notes:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;I have included the worker thread name in the output so that I could see that parallelism was taking effect and that different threads were performing different amounts of processing.&lt;/li&gt;&lt;li&gt;The &lt;font face="courier new,courier"&gt;Thread.Sleep(1)&lt;/font&gt; call in the beginning of the Fibonacci implementation is there just to put the current thread to sleep so the operating system scheduler could switch processing to another thread. This helps force parallelism on my single-core virtual development machine.&lt;/li&gt;&lt;li&gt;The implementation of the Fibonacci algorithm is not ideal, but is good enough for a small sample application.&lt;/li&gt;&lt;/ul&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=2559" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="Parallel Extensions to the .NET Framework" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Parallel+Extensions+to+the+.NET+Framework/default.aspx" /></entry><entry><title>Dual-Threaded TaskManager on a Single Core</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2008/03/17/Dual_2D00_Threaded-TaskManager-on-a-Single-Core.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2008/03/17/Dual_2D00_Threaded-TaskManager-on-a-Single-Core.aspx</id><published>2008-03-18T01:57:00Z</published><updated>2008-03-18T01:57:00Z</updated><content type="html">&lt;p&gt;I just ran the following code on a single core machine:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Task Task1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Task Task2;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Action&amp;lt;object&amp;gt; MainAction;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TaskManagerPolicy CurrentPolicy;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MainAction = ActionMethod;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CurrentPolicy = new TaskManagerPolicy(2, 2);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TaskManager CurrentTaskManager = new TaskManager(CurrentPolicy);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Task1 = Task.Create(MainAction, CurrentTaskManager);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Task2 = Task.Create(MainAction, CurrentTaskManager);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Task1.Wait();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Task2.Wait();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static void ActionMethod(object o)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&amp;quot;Thread Name: {0}&amp;quot;, Thread.CurrentThread.Name);&lt;br /&gt;}&lt;/font&gt;&lt;br /&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;My intent with this code was to see the two tasks executing on separate threads. Since the default &lt;font face="courier new,courier"&gt;TaskManager&lt;/font&gt; uses one thread per processor, and since I was running the code on a single core machine, the default &lt;font face="courier new,courier"&gt;TaskManager&lt;/font&gt; would have executed both tasks on a single thread. In the code above, I used a &lt;font face="courier new,courier"&gt;TaskManagerPolicy&lt;/font&gt; object to specify a minimum of two threads and an ideal thread count of two, and I constructed a new &lt;font face="courier new,courier"&gt;TaskManager&lt;/font&gt; using that policy.&lt;/p&gt;&lt;p&gt;Imagine my surprise&amp;nbsp;when&amp;nbsp;the output was as follows:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;Thread Name: Worker 1&lt;br /&gt;Thread Name: Worker 1&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Both tasks ran on the same thread! This wasn&amp;#39;t what I was expecting, especially since I explicitly asked for two threads in my task manager policy object.&lt;/p&gt;&lt;p&gt;Then, I modified the &lt;font face="courier new,courier"&gt;ActionMethod&lt;/font&gt; to put the task to sleep:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="Courier New"&gt;static void ActionMethod(object o)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&amp;quot;Thread Name: {0}&amp;quot;, Thread.CurrentThread.Name);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;&lt;font color="#ff0000"&gt;Thread.Sleep(5000);&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Then, my output changed to what I was expecting:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="Courier New"&gt;Thread Name: Worker 1&lt;br /&gt;Thread Name: Worker 2&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;What was the difference? The difference has to do with issues with&amp;nbsp;threads and blocking.&lt;/p&gt;&lt;p&gt;When tasks are created, they are placed in a work queue that is managed by the associated &lt;font face="courier new,courier"&gt;TaskManager&lt;/font&gt; object. The &lt;font face="courier new,courier"&gt;TaskManager&lt;/font&gt; object also creates the requested two worker threads. Adding the tasks to the &lt;font face="courier new,courier"&gt;TaskManager&lt;/font&gt; object&amp;#39;s work queue signals one of the worker threads&amp;nbsp;that an item is available and the worker thread begins running the task. Since there is, in my situation,&amp;nbsp;only one core, the &lt;font face="courier new,courier"&gt;TaskManager&lt;/font&gt; object has no way of notifying the second worker thread of the existence of &lt;font face="courier new,courier"&gt;Task2&lt;/font&gt;, until &lt;font face="courier new,courier"&gt;Task1&lt;/font&gt; is finished or blocks. Without the call to &lt;font face="courier new,courier"&gt;Thread.Sleep()&lt;/font&gt;, as in my first example,&amp;nbsp;Worker Thread 1&amp;nbsp;finishes &lt;font face="courier new,courier"&gt;Task1&lt;/font&gt; and the &lt;font face="courier new,courier"&gt;TaskManager&lt;/font&gt; object signals Worker Thread 1 again to start &lt;font face="courier new,courier"&gt;Task2&lt;/font&gt;.&lt;/p&gt;&lt;p&gt;With the call to &lt;font face="courier new,courier"&gt;Thread.Sleep()&lt;/font&gt;, as in my second example, Worker&amp;nbsp;Thread 1&amp;nbsp;blocks, and the operating system scheduler switches to a runable thread. The &lt;font face="courier new,courier"&gt;TaskManager&lt;/font&gt; object can now signal&amp;nbsp;Worker Thread 2&amp;nbsp;to start &lt;font face="courier new,courier"&gt;Task2&lt;/font&gt;.&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=2554" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="Parallel Extensions to the .NET Framework" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Parallel+Extensions+to+the+.NET+Framework/default.aspx" /></entry><entry><title>Aggregated Exceptions</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2008/03/12/Aggregated-Exceptions.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2008/03/12/Aggregated-Exceptions.aspx</id><published>2008-03-12T03:26:00Z</published><updated>2008-03-12T03:26:00Z</updated><content type="html">&lt;p&gt;Running code in parallel raises the possibility that more than one exception may be thrown, in parallel, from a single body of code. This means that code written to use the Parallel Extensions needs to account for that possibility.&lt;/p&gt;&lt;p&gt;Consider the following sequential, imperative&amp;nbsp;&lt;font face="courier new,courier"&gt;for&lt;/font&gt; loop:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;for(i = 0; i &amp;lt; 1000; i++)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((i % 2) != 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new NotSupportedException();&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;This loop is set to execute 1,000 iterations of a loop. When the iteration counter is not evenly divisible by two, the code throws an exception. Given the sequential nature of this loop, the first (and only) exception will be thrown when the value of &lt;font face="courier new,courier"&gt;i&lt;/font&gt; is 1. At that point, the loop will stop and the stack will unwind to the first available handler for the exception. No magic here - that&amp;#39;s straightforward enough. One exception is all that is available, and all that is needed.&lt;/p&gt;&lt;p&gt;But what will happen when this loop runs in parallel? Consider this parallel version of the same algorithm:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;Parallel.For(0, 1000, i =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((i % 2) != 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new NotSupportedException();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;);&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Multiple threads will be spun up to handle this algorithm, which means that multiple threads will be throwing instances of the &lt;font face="courier new,courier"&gt;NotSupportedException&lt;/font&gt; class in parallel. How will you catch them all?&lt;/p&gt;&lt;p&gt;The good news is that you don&amp;#39;t have to. The Parallel Extensions supports a new exception type called &lt;font face="courier new,courier"&gt;System.Threading.AggregateException&lt;/font&gt;, that aggregates all exceptions thrown from threads spun up to do parallel work. This gives you a single exception to catch, which means that you can write a &lt;font face="courier new,courier"&gt;try&lt;/font&gt;/&lt;font face="courier new,courier"&gt;catch&lt;/font&gt; block in the same manner that you do today, without having to worry about catching multiple exceptions raised from threads that you didn&amp;#39;t even directly spin up in the first place.&lt;/p&gt;&lt;p&gt;The &lt;font face="courier new,courier"&gt;AggregateException&lt;/font&gt; class contains a property called &lt;font face="courier new,courier"&gt;InnerExceptions&lt;/font&gt;, which is of type &lt;font face="courier new,courier"&gt;System.Collections.ObjectModel.ReadOnlyCollection&amp;lt;Exception&amp;gt;&lt;/font&gt;. This collection, as you might guess, contains all of the exception objects thrown from various threads that were spun up to do work in parallel.&lt;/p&gt;&lt;p&gt;Given that, you can write an exception management block for that same parallel loop using something like this:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;try&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Parallel.For(0, 1000, i =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((i % 2) != 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new NotSupportedException();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; );&lt;br /&gt;&lt;/font&gt;&lt;font face="courier new,courier"&gt;}&lt;br /&gt;catch (System.Threading.AggregateException AggEx)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&amp;quot;Inner Exceptions: {0}&amp;quot;, AggEx.InnerExceptions.Count);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int i = 0; i &amp;lt; AggEx.InnerExceptions.Count; i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&amp;quot;* {0}: {1}&amp;quot;, i, AggEx.InnerExceptions[i].GetType().ToString());&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;When I run this code on my Dell Latitude D620 dual-core laptop, I get the following output:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;Inner Exceptions: 2&lt;br /&gt;* 0: System.NotSupportedException&lt;br /&gt;* 1: System.NotSupportedException&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=2461" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="Parallel Extensions to the .NET Framework" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Parallel+Extensions+to+the+.NET+Framework/default.aspx" /></entry><entry><title>PFX Dec 2007 CTP: Executing Tasks Sequentially</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2008/03/11/PFX-Dec-2007-CTP_3A00_-Executing-Tasks-Sequentially.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2008/03/11/PFX-Dec-2007-CTP_3A00_-Executing-Tasks-Sequentially.aspx</id><published>2008-03-11T04:16:00Z</published><updated>2008-03-11T04:16:00Z</updated><content type="html">&lt;p&gt;An &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2674628&amp;amp;SiteID=1"&gt;MSDN&amp;nbsp;forum post from a couple of months ago&lt;/a&gt; (OK, so I am slow) asked if it was possible to schedule a task for execution after a previous task executed. &lt;a href="http://blogs.msdn.com/toub/"&gt;Stephen Toub&lt;/a&gt; noted that handling the &lt;font face="courier new,courier"&gt;Completed&lt;/font&gt; event on a &lt;font face="courier new,courier"&gt;Task&lt;/font&gt; object would be the key to making such a design possible.&lt;/p&gt;&lt;p&gt;I have taken Stephen&amp;#39;s design pattern and have implemented it in code. My fairly straightforward code sample is &lt;a href="http://cid-7062094a08532907.skydrive.live.com/self.aspx/Public/Post2674628.zip"&gt;here&lt;/a&gt;&amp;nbsp;in a ZIP file that contains a Visual Studio 2008 solution that illustrates the pattern. The driving force in the sample is in my implementation of a simple &lt;font face="courier new,courier"&gt;TaskCoordinator&lt;/font&gt; replacement that implements a new method called &lt;font face="courier new,courier"&gt;WaitAllSequential()&lt;/font&gt;:&lt;/p&gt;&lt;font size="2"&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;Post2674628.&lt;/font&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#2b91af" size="2"&gt;TaskCoordinator&lt;/font&gt;&lt;font size="2"&gt; tc = &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;font size="2"&gt; Post2674628.&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;TaskCoordinator&lt;/font&gt;&lt;/font&gt;&lt;font face="courier new,courier"&gt;&lt;font size="2"&gt;();&lt;br /&gt;tc.WaitAllSequential(&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;Task&lt;/font&gt;&lt;font size="2"&gt;[] { Task1, Task2, Task3 });&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;/font&gt;&lt;p&gt;Unlike the static &lt;font face="courier new,courier"&gt;TaskCoordinator&lt;/font&gt; class that ships with the Dec 2007 CTP of the Parallel Extensions to the .NET Framework, my &lt;font face="courier new,courier"&gt;TaskCoordinator&lt;/font&gt; needs an object instance with which to work. I arrived at this conclusion after diving, for a bit, into extension methods. My original hope was to add a static extension method onto the existing &lt;font face="courier new,courier"&gt;TaskCoordinator&lt;/font&gt; class to handle the sequential task exection:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="Courier New"&gt;TaskCoordinator.WaitAllSequential(&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;Task&lt;/font&gt;&lt;font size="2"&gt;[] { Task1, Task2, Task3 });&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;This is because C&lt;sup&gt;#&lt;/sup&gt; (and, I presume, VB.NET) does not the authoring support static extension methods (this is discussed further in &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2046738&amp;amp;SiteID=1"&gt;another MSDN forum post&lt;/a&gt;). This design would match nicely with the existing design of &lt;font face="courier new,courier"&gt;WaitAll()&lt;/font&gt;:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;TaskCoordinator.WaitAll(new Task[] { t1, t2, t3 });&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;But, without support for static extension methods,&amp;nbsp;I&amp;#39;m left with requiring an instance. However, the &lt;font face="courier new,courier"&gt;TaskCoordinator&lt;/font&gt; class is an abstract class, so I can&amp;#39;t write:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;TaskCoordinator tc = new TaskCoordinator();&lt;br /&gt;tc.WaitAllSequential(new Task[] { t1, t2, t3 });&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Furthermore, I can&amp;#39;t use a &lt;font face="courier new,courier"&gt;Task&lt;/font&gt; object, even though &lt;font face="courier new,courier"&gt;Task&lt;/font&gt; derives from &lt;font face="courier new,courier"&gt;TaskCoordinator&lt;/font&gt;:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;TaskCoordinator tc = new Task();&lt;br /&gt;tc.WaitAllSequential(new Task[] { t1, t2, t3 });&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;This is because the &lt;font face="courier new,courier"&gt;Task&lt;/font&gt; class does not have a parameterless constructor, and it seems like overkill to construct a valid &lt;font face="courier new,courier"&gt;Task&lt;/font&gt; object just to get all of this to work. Given all of this, then, the code design simply reverts to implementing a static class called &lt;font face="courier new,courier"&gt;TaskCoordinator&lt;/font&gt; in a different namespace.&lt;br /&gt;&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=2452" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="Parallel Extensions to the .NET Framework" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Parallel+Extensions+to+the+.NET+Framework/default.aspx" /></entry><entry><title>Twin Cities Code Camp: Spring 2008</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2008/03/05/Twin-Cities-Code-Camp_3A00_-Spring-2008.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2008/03/05/Twin-Cities-Code-Camp_3A00_-Spring-2008.aspx</id><published>2008-03-05T14:38:00Z</published><updated>2008-03-05T14:38:00Z</updated><content type="html">&lt;p&gt;I wanted to put out an advertisement for the Spring 2008 session of the Twin Cities Code Camp. You can go &lt;a href="http://www.twincitiescodecamp.com/TCCC/Spring2008/Schedule.aspx"&gt;here&lt;/a&gt; for a list of sessions. I will be speaking about the Parallel Extensions to the .NET Framework, and many other excellent sessions are planned as well. For more information about the Twin Cities Code Camp, go &lt;a href="http://www.twincitiescodecamp.com/TCCC/"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&amp;nbsp;See you on April 5!&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=2400" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="Twin Cities Code Camp" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Twin+Cities+Code+Camp/default.aspx" /><category term="Parallel Extensions to the .NET Framework" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Parallel+Extensions+to+the+.NET+Framework/default.aspx" /></entry><entry><title>A WPF-Based NFA Viewer for Sotue</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2007/05/14/A-WPF_2D00_Based-NFA-Viewer-for-Sotue.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2007/05/14/A-WPF_2D00_Based-NFA-Viewer-for-Sotue.aspx</id><published>2007-05-15T01:31:00Z</published><updated>2007-05-15T01:31:00Z</updated><content type="html">&lt;p&gt;My work in building NFAs from regular expressions is nearly complete; many constructs are supported. I have a variety of unit tests built to test the&amp;nbsp;construction of NFAs for various constructs (such as the OR operator in&amp;nbsp;a regular expression of &amp;quot;a|b&amp;quot;), and those tests are passing. I thought that it might be interesting to visualize these NFAs, so I have constructed a simple viewer using Windows Presentation Foundation (WPF):&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;img align="middle" alt="Sotue NFA Viewer: May 14 2007" height="342" src="http://i210.photobucket.com/albums/bb118/JeffF-Magenic/NfaViewer-May-14-2007.jpg" style="width:705px;height:342px;" title="Sotue NFA Viewer: May 14 2007" width="705" /&gt;&lt;/p&gt;&lt;p&gt;All in all, WPF has made this task pretty simple. With WPF, I have been able to do the following:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;draw states with the WPF &lt;font face="courier new,courier"&gt;Ellipse&lt;/font&gt; class&lt;/li&gt;&lt;li&gt;use radial gradient shadings to give a 3D-ish look to the state ellipses (with the NFA&amp;#39;s end state shaded in green)&lt;/li&gt;&lt;li&gt;draw transition lines between shapes with the WPF &lt;font face="courier new,courier"&gt;Line&lt;/font&gt; class&lt;/li&gt;&lt;li&gt;provide tool tips for the states that list the state&amp;#39;s ID&lt;/li&gt;&lt;li&gt;provide tool tips for the state transition lines that list the destination state for the transition line and the character (if applicable) that will move the machine from the source state to the destination state&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;This was a good exercise for working with WPF using C&lt;sup&gt;#&lt;/sup&gt;. Since the NFAs are generated on the fly by the Sotue engine, the NFA cannot be drawn in XAML by a designer but instead must be drawn on the fly with C&lt;sup&gt;#&lt;/sup&gt; codebehind goodness. In general, this WPF-based approach was easier than performing the same task with GDI+ -- more importantly, I got a chance to work with WPF.&lt;/p&gt;&lt;p&gt;I&amp;#39;ve got more work to do on the viewer, such as scaling the NFA to fit the canvas&amp;#39; contents, and getting arrowheads rotated properly for state transition lines that are not horizontal, and better state placement decisions for layout, but I&amp;#39;m off to a fair enough start.&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=116" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="Sotue" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Sotue/default.aspx" /><category term="WPF" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/WPF/default.aspx" /></entry><entry><title>Fixing The TF30255 Error During TFS Project Creation</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2007/03/09/Fixing-The-TF30255-Error-During-TFS-Project-Creation.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2007/03/09/Fixing-The-TF30255-Error-During-TFS-Project-Creation.aspx</id><published>2007-03-09T17:13:00Z</published><updated>2007-03-09T17:13:00Z</updated><content type="html">&lt;p&gt;Over the last few days, I have been struggling with creating a new project on my TFS installation.&amp;nbsp; The project creation operation would fail about 85% of the way through the process with an error which read &lt;strong&gt;TF30255: Error uploading report : Work Item With Tasks&lt;/strong&gt;. Apparently, TFS was attempting to upload report templates to SQL Server 2005 Reporting Services and was failing.&lt;/p&gt;&lt;p&gt;I was finally able to resolve the issue today and wanted to document my fixes:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;add a role called &lt;font face="courier new,courier"&gt;R&lt;/font&gt;&lt;font face="courier new,courier"&gt;SExecRole&lt;/font&gt; to the &lt;font face="courier new,courier"&gt;master&lt;/font&gt; database&lt;/li&gt;&lt;li&gt;add a role called &lt;font face="courier new,courier"&gt;R&lt;/font&gt;&lt;font face="courier new,courier"&gt;SExecRole&lt;/font&gt; to the &lt;font face="courier new,courier"&gt;msdb&lt;/font&gt; database&lt;/li&gt;&lt;li&gt;add permissions to the &amp;quot;Securables&amp;quot; section of the &lt;font face="courier new,courier"&gt;RSExecRole&lt;/font&gt; in the &lt;font face="courier new,courier"&gt;master&lt;/font&gt; database&lt;/li&gt;&lt;ul&gt;&lt;li&gt;add the &lt;font face="courier new,courier"&gt;execute&lt;/font&gt; permisssion for the extended stored procedure called &lt;font face="courier new,courier"&gt;xp_sqlagent_notify&lt;/font&gt;&lt;/li&gt;&lt;li&gt;add the &lt;font face="courier new,courier"&gt;execute&lt;/font&gt; permisssion for the extended stored procedure called &lt;font face="courier new,courier"&gt;xp_sqlagent_enum_jobs&lt;/font&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;add permissions to the &amp;quot;Securables&amp;quot; section of the &lt;font face="courier new,courier"&gt;RSExecRole&lt;/font&gt; in the &lt;font face="courier new,courier"&gt;msdb&lt;/font&gt; database&lt;/li&gt;&lt;ul&gt;&lt;li&gt;add the &lt;font face="courier new,courier"&gt;execute&lt;/font&gt; permisssion for the stored procedure called &lt;font face="courier new,courier"&gt;sp_add_category&lt;/font&gt;&lt;/li&gt;&lt;li&gt;add the &lt;font face="courier new,courier"&gt;execute&lt;/font&gt; permisssion for the stored procedure called &lt;font face="courier new,courier"&gt;sp_verify_job_identifiers&lt;/font&gt;&lt;/li&gt;&lt;li&gt;add the &lt;font face="courier new,courier"&gt;select&lt;/font&gt; permission for the table called &lt;font face="courier new,courier"&gt;sysjobs&lt;/font&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;p&gt;I&amp;#39;m guessing this was something I missed in the TFS Installation Guide. I really need to stop being lazy and read before I install. Thanks goes to &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=662319&amp;amp;SiteID=1"&gt;this post&lt;/a&gt; for helping me out on this thorny problem.&lt;/p&gt;&lt;p&gt;&amp;nbsp;Now I can create a TFS project for Sotue ...&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=60" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="TFS" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/TFS/default.aspx" /></entry><entry><title>TFS Installations and SQL Server 2005 Collations</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2007/03/07/TFS-Installations-and-SQL-Server-2005-Collations.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2007/03/07/TFS-Installations-and-SQL-Server-2005-Collations.aspx</id><published>2007-03-07T17:30:00Z</published><updated>2007-03-07T17:30:00Z</updated><content type="html">&lt;p&gt;I&amp;#39;ve just installed Team Foundation Server (TFS) in a Windows 2003 Standard R2 VPC, and I can say that the biggest challenge had to do with the collation settings in the SQL Server 2005 instance used by TFS. The TFS Installation Guide mentions the importance of the correct collation, and this point is not to be underestimated.&lt;/p&gt;&lt;p&gt;Before TFS installs, it runs a system check to ensure that all of its prerequisites are in place. One of those checks has to do with the collation settings on the default SQL Server 2005 instance. It&amp;#39;s important to know that TFS does not support case sensitive collation settings, nor does it support accent sensitive collation settings. My issue was that, by the time I had figured this out, I had already installed SQL Server 2005, Analysis Services, Reporting Services and Sharepoint 2.0. Worse yet, the SQL Server 2005 Management Studio does not allow you to change collation settings on a server after installation. Yikes! I was faced with uninstalling and re-installing SQL Server 2005 just to change the collation settings to make TFS happy.&lt;/p&gt;&lt;p&gt;Luckily, some digging found a somewhat easier answer. I had the option of reinstalling the SQL Engine and specifying the new collation during the reinstallation. I mounted the SQL Server 2005 ISO into my VPC, opened a command prompt, moved to the &lt;font face="courier new,courier"&gt;\Servers&lt;/font&gt; directory on the mounted ISO, and ran the following:&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;D:\Servers&amp;gt;&lt;strong&gt;start /wait setup.exe /qb INSTANCENAME=MSSQLSERVER REINSTALL=SQL_Engine REBUILDDATABASE=1 SAPWD=&amp;lt;&lt;em&gt;SA password&lt;/em&gt;&amp;gt; SQLCOLLATION=SQL_Latin1_General_CP1_CI_AS&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;&lt;p&gt;Voila! All better, and I avoided a full reinstall.&lt;/p&gt;&lt;p&gt;Note that, when you run this command, you will be detaching any existing databases. Since TFS requires Reporting Services, I had the &lt;font face="courier new,courier"&gt;ReportServer&lt;/font&gt; and &lt;font face="courier new,courier"&gt;ReportServerTempDB&lt;/font&gt; databases attached to the default SQL Server instance before I reran &lt;font face="courier new,courier"&gt;setup.exe&lt;/font&gt; from the command line, and the databases were gone after &lt;font face="courier new,courier"&gt;setup.exe&lt;/font&gt; completed. The databases are simply detached, and not deleted, and they can be re-attached with ease once &lt;font face="courier new,courier"&gt;setup.exe&lt;/font&gt; completes.&lt;/p&gt;&lt;p&gt;If you&amp;#39;re going to install TFS, do yourself a favor and download the Installation Guide and follow it to the letter. It will save you heartache later on.&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=58" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="TFS" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/TFS/default.aspx" /></entry><entry><title>NFA Generation</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2007/03/06/NFA-Generation.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2007/03/06/NFA-Generation.aspx</id><published>2007-03-06T13:43:00Z</published><updated>2007-03-06T13:43:00Z</updated><content type="html">&lt;p&gt;Lexical analysis involves searching input data for lexemes that match tokens specified as regular expressions. This is enabled by building a state machine for each token, and then running each input character through the state machines looking for matches.&lt;/p&gt;&lt;p&gt;&amp;nbsp;Sotue&amp;#39;s approach to state machine generation will follow the process defined in &lt;a href="http://www.holub.com/company/allen_holub.html" title="Allen Holub"&gt;Allen Holub&lt;/a&gt;&amp;#39;s excellent, though now out of print, &lt;em&gt;&lt;a href="http://www.holub.com/software/compiler.design.in.c.html" title="Compiler Design in C"&gt;Compiler Design In C&lt;/a&gt;&lt;/em&gt;. This process, at a high level, is as follows:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Generate a non-deterministic finite automata (NFA) from a regular expression&lt;/li&gt;&lt;li&gt;Optimize the non-deterministic finite automata (NFA) into a deterministic finite automata (DFA)&lt;/li&gt;&lt;li&gt;Use the deterministic finite automata (DFA) as the state machine for input stream lexeme analysis&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The Sotue engine can already parse regular expressions and I have begun work to build the NFAs from the regular expressions. I am working on supporting the following constructs in regular expressions defined in Sotue tokens:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;OR operators (a regular expression token of &lt;font face="courier new,courier"&gt;a|b&lt;/font&gt; matches input of &lt;font face="courier new,courier"&gt;a&lt;/font&gt; or &lt;font face="courier new,courier"&gt;b&lt;/font&gt;)&lt;/li&gt;&lt;li&gt;wildcard characters (a regular expression token &lt;font face="courier new,courier"&gt;a.y&lt;/font&gt; matches input of &lt;font face="courier new,courier"&gt;any&lt;/font&gt;, &lt;font face="courier new,courier"&gt;amy&lt;/font&gt; and the &lt;font face="courier new,courier"&gt;agy&lt;/font&gt; in &lt;font face="courier new,courier"&gt;maygar&lt;/font&gt;)&lt;/li&gt;&lt;li&gt;Start-of line anchors (a regular expression token of &lt;font face="courier new,courier"&gt;^and&lt;/font&gt; matches input of &lt;font face="courier new,courier"&gt;and&lt;/font&gt; only if the &lt;font face="courier new,courier"&gt;and&lt;/font&gt; appears at the beginning of a line)&lt;/li&gt;&lt;li&gt;End-of line anchors (a regular expression token of &lt;font face="courier new,courier"&gt;and$&lt;/font&gt; matches input of &lt;font face="courier new,courier"&gt;and&lt;/font&gt; only if the &lt;font face="courier new,courier"&gt;and&lt;/font&gt; appears at the end of a line)&lt;/li&gt;&lt;li&gt;Inclusive character classes (a regular expression token of &lt;font face="courier new,courier"&gt;[A-Z]&lt;/font&gt; matches input matches any character in the range of &lt;font face="courier new,courier"&gt;A&lt;/font&gt; through &lt;font face="courier new,courier"&gt;Z&lt;/font&gt;, inclusive)&lt;/li&gt;&lt;li&gt;Exclusive character classes (a regular expression token of &lt;font face="courier new,courier"&gt;[^A-Z]&lt;/font&gt; matches input matches any character&amp;nbsp;outside of&amp;nbsp;the range of &lt;font face="courier new,courier"&gt;A&lt;/font&gt; through &lt;font face="courier new,courier"&gt;Z&lt;/font&gt;, inclusive)&lt;/li&gt;&lt;li&gt;Zero-or-more closure operators (a regular expression token of &lt;font face="courier new,courier"&gt;a*&lt;/font&gt; matches &lt;font face="courier new,courier"&gt;a&lt;/font&gt;, &lt;font face="courier new,courier"&gt;aa&lt;/font&gt;, &lt;font face="courier new,courier"&gt;aaa&lt;/font&gt; and the empty string)&lt;/li&gt;&lt;li&gt;One-or-more closure operators (a regular expression token of &lt;font face="courier new,courier"&gt;a+&lt;/font&gt; matches &lt;font face="courier new,courier"&gt;a&lt;/font&gt;, &lt;font face="courier new,courier"&gt;aa&lt;/font&gt;, and &lt;font face="courier new,courier"&gt;aaa&lt;/font&gt; but not the empty string)&lt;/li&gt;&lt;li&gt;Zero-or-one closure operators (a regular expression token of &lt;font face="courier new,courier"&gt;a?&lt;/font&gt; matches &lt;font face="courier new,courier"&gt;a&lt;/font&gt; and the empty string but not &lt;font face="courier new,courier"&gt;aa&lt;/font&gt; and not &lt;font face="courier new,courier"&gt;aaa&lt;/font&gt;)&lt;/li&gt;&lt;li&gt;Groups (a regular expression token of &lt;font face="courier new,courier"&gt;(ab)*&lt;/font&gt; matches the grouped expression &lt;font face="courier new,courier"&gt;ab&lt;/font&gt; zero or more times)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I am now working on building NFAs for each of these situations. I am, in parallel, working on building a small WPF app that will draw out the NFA for a regular expression as a series of circled states connected by lines.&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=55" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="Sotue" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Sotue/default.aspx" /></entry><entry><title>The Sotue Vision: Lex and Yacc the .NET Way</title><link rel="alternate" type="text/html" href="http://blog.magenic.com/blogs/jefff/archive/2007/02/26/The-Sotue-Vision_3A00_-Lex-and-Yacc-the-.NET-Way.aspx" /><id>http://blog.magenic.com/blogs/jefff/archive/2007/02/26/The-Sotue-Vision_3A00_-Lex-and-Yacc-the-.NET-Way.aspx</id><published>2007-02-26T16:48:00Z</published><updated>2007-02-26T16:48:00Z</updated><content type="html">&lt;p&gt;The &lt;strong&gt;lex&lt;/strong&gt; tool is &lt;a href="http://en.wikipedia.org/wiki/Lex_programming_tool" title="Lex, as defined in Wikipedia"&gt;defined in Wikipedia&lt;/a&gt; as follows:&lt;/p&gt;&lt;p&gt;&lt;em&gt;lex is a program that generates lexical analyzers (&amp;quot;scanners&amp;quot; or &amp;quot;lexers&amp;quot;). Lex is commonly used with the yacc parser generator. Lex, originally written by Eric Schmidt and Mike Lesk, is the standard lexical analyzer generator on Unix systems, and is included in the POSIX standard. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;The tool accepts an input file that describes the patterns that it needs to find in the input stream. The input file looks something like this:&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;/*** Definition section ***/&lt;br /&gt;&lt;br /&gt;%{&lt;br /&gt;/* C code to be copied verbatim */&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;%}&lt;br /&gt;&lt;br /&gt;/* This tells flex to read only one input file */&lt;br /&gt;%option noyywrap&lt;br /&gt;&lt;br /&gt;%%&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*** Rules section ***/&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* [0-9]+ matches a string of one or more digits */&lt;br /&gt;[0-9]+&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* yytext is a string containing the matched text. */&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&amp;quot;Saw an integer: %s\n&amp;quot;, yytext);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp; /* Ignore all other characters. */&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;%%&lt;br /&gt;/*** C Code section ***/&lt;br /&gt;&lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Call the lexer, then quit. */&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; yylex();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;&lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;&lt;p&gt;The &lt;strong&gt;lex&lt;/strong&gt; tool generates C code that can be folded into a larger application and used to parse the application&amp;#39;s input.&lt;/p&gt;&lt;p&gt;This all made sense in 1970, when Unix systems were the dominant operating system of choice for software development, and C was the dominant language of choice.&amp;nbsp;But it&amp;#39;s 2007 now, and many of us are on desktops with .NET runtimes. We&amp;#39;re working in Unicode now, and not just ASCII. We&amp;#39;re working in C&lt;sup&gt;#&lt;/sup&gt; and VB.NET now, and not just C (in fact, we&amp;#39;re probably not working in C much at all). Our brave new .NET world (and by &amp;quot;new&amp;quot; I mean &amp;quot;six and a half years old&amp;quot;) has moved beyond cryptic input files and unmaintainable generated code and into a language-agnostic and dynamic environment. I believe that the .NET world can do better. Projects like &lt;a href="http://sourceforge.net/projects/csflex"&gt;C&lt;sup&gt;#&lt;/sup&gt; Flex&lt;/a&gt; exist, but, in my opinion, don&amp;#39;t go far enough. I believe that a fresh approach to lexical analysis and grammatical parsing is needed (of course, I also believe that people should stop putting two spaces after their periods when they type, so my beliefs may often be called into question).&lt;/p&gt;&lt;p&gt;My vision is to create a lexical analysis and grammatical parsing engine, from the ground up, designed for the .NET platform and all of its language agnostic, Unicode-aware goodness. The engine, which I have dubbed &lt;strong&gt;&lt;em&gt;Sotue&lt;/em&gt;&lt;/strong&gt; (no,&amp;nbsp;the name&amp;nbsp;doesn&amp;#39;t mean anything), would provide a .NET assembly (or set of assemblies) that would contain classes that would handle all of the details. My ultra-high-level vision looks something like this:&lt;/p&gt;&lt;p&gt;&lt;font face="courier new,courier"&gt;LexicalAnalyzer MyParser = new LexicalAnalyzer();&lt;br /&gt;&lt;br /&gt;Token NumericToken = new Token(&amp;quot;[0-9]+&amp;quot;);&lt;br /&gt;NumericToken.OnLexemeFound += &lt;em&gt;&amp;lt;&lt;/em&gt;&lt;/font&gt;&lt;em&gt;delegate&lt;/em&gt;&lt;font face="courier new,courier"&gt;&lt;em&gt;&amp;gt;&lt;/em&gt;;&lt;br /&gt;MyParser.Tokens.Add(NumericToken);&lt;br /&gt;&lt;br /&gt;MyParser.Parse(&lt;/font&gt;&lt;em&gt;&amp;lt;input stream object implementing &lt;font face="courier new,courier"&gt;IStream&lt;/font&gt;&amp;gt;&lt;/em&gt;&lt;font face="courier new,courier"&gt;);&lt;/font&gt;&lt;/p&gt;&lt;p&gt;(That&amp;#39;s a C&lt;sup&gt;#&lt;/sup&gt; sample, but, since the vision is to supply a .NET assembly, the classes will support any .NET language.)&lt;/p&gt;&lt;p&gt;The &lt;font face="courier new,courier"&gt;Token&lt;/font&gt; class would represent an input pattern and the &lt;font face="courier new,courier"&gt;OnLexemeFound&lt;/font&gt; event would fire when data matching the token&amp;#39;s regular expression was found in an input stream. (In lexical analysis terms, a &lt;em&gt;lexeme&lt;/em&gt; is a string of data that matches a regular expression defined by a &lt;em&gt;token&lt;/em&gt;; for example, a lexeme of &lt;font face="courier new,courier"&gt;123&lt;/font&gt; would match a token with a regular expression of &lt;font face="courier new,courier"&gt;[0-9]+&lt;/font&gt;.)&lt;/p&gt;&lt;p&gt;I started work on this vision once before, but its progress was hampered by all of life&amp;#39;s little distractions. I have more time now, and I have time to re-engage. Work towards this Sotue vision is already underway, and I&amp;#39;ll keep this blog updated with my progress.&lt;/p&gt;&lt;p&gt;Welcome to Sotue.&lt;/p&gt;&lt;img src="http://blog.magenic.com/aggbug.aspx?PostID=43" width="1" height="1"&gt;</content><author><name>jefff</name><uri>http://blog.magenic.com/members/jefff.aspx</uri></author><category term="Sotue" scheme="http://blog.magenic.com/blogs/jefff/archive/tags/Sotue/default.aspx" /></entry></feed>