{"id":2913,"date":"2007-12-10T02:06:00","date_gmt":"2007-12-10T02:06:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/pfxteam\/2007\/12\/10\/code-snippets-for-parallel-for-and-parallel-foreach\/"},"modified":"2007-12-10T02:06:00","modified_gmt":"2007-12-10T02:06:00","slug":"code-snippets-for-parallel-for-and-parallel-foreach","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/code-snippets-for-parallel-for-and-parallel-foreach\/","title":{"rendered":"Code Snippets for Parallel.For and Parallel.ForEach"},"content":{"rendered":"<p><P>Introduced in Visual Studio 2005, <A href=\"http:\/\/msdn.microsoft.com\/vstudio\/tour\/vs2005_guided_tour\/VS2005pro\/Smart_Client\/CPlusIntellisense.htm\">Code Snippets<\/A> allow you to quickly insert reusable blocks of code into your project.&nbsp; For example, if&nbsp;you want to quickly write a for loop in C#,&nbsp;you can simply type &#8220;for&#8221; into your code file, and IntelliSense shows you the &#8220;for&#8221; code snippet:<\/P>\n<P><A href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/21\/2019\/02\/image.png\"><IMG height=\"193\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/21\/2019\/02\/image_thumb.png\" width=\"479\"><\/A> <\/P>\n<P>Now&nbsp;you press the tab key twice, and Visual Studio automatically expands this for you into a for loop, allowing you to quickly replace things like the iteration variable&#8217;s name:<\/P>\n<P><A href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/21\/2019\/02\/image_1.png\"><IMG height=\"145\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/21\/2019\/02\/image_thumb_1.png\" width=\"339\"><\/A> <\/P>\n<P>One of the coolest things about Code Snippets, however, is that you can write your own.&nbsp; Imagine being able to type &#8220;parall&#8221; and have a Code Snippet for Parallel.For pop up:<\/P>\n<P><A href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/21\/2019\/02\/image_2.png\"><IMG height=\"190\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/21\/2019\/02\/image_thumb_2.png\" width=\"530\"><\/A> <\/P>\n<P>When you expand this,&nbsp;you&#8217;d get a Parallel.For loop:<\/P>\n<P><A href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/21\/2019\/02\/image_3.png\"><IMG height=\"155\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/21\/2019\/02\/image_thumb_3.png\" width=\"297\"><\/A> <\/P>\n<P>&#8220;Magic,&#8221; you say?&nbsp; Not quite.&nbsp; All&nbsp;you have to do is write a custom snippet that looks as follows:<\/P>\n<BLOCKQUOTE>\n<P>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243; ?&gt;<BR>&lt;CodeSnippets&nbsp; xmlns=&#8221;<A href=\"http:\/\/schemas.microsoft.com\/VisualStudio\/2005\/CodeSnippet%22\">http:\/\/schemas.microsoft.com\/VisualStudio\/2005\/CodeSnippet&#8221;<\/A>&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;CodeSnippet Format=&#8221;1.0.0&#8243;&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Header&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Title&gt;parallelfor&lt;\/Title&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Shortcut&gt;parallelfor&lt;\/Shortcut&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Description&gt;Code snippet for &#8216;Parallel.For&#8217; loop&lt;\/Description&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;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sp;&nbsp;&nbsp;&nbsp; &lt;Default&gt;to&lt;\/Default&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ToolTip&gt;To&lt;\/ToolTip&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/Literal&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/Declarations&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Code Language=&#8221;csharp&#8221;&gt;&lt;![CDATA[Parallel.For($from$, $to$, $index$ =&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $selected$ $end$<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });]]&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/Code&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/Snippet&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;\/CodeSnippet&gt;<BR>&lt;\/CodeSnippets&gt;<\/P><\/BLOCKQUOTE>\n<P>Save that to a file parallelfor.snippet in a directory of your choosing.&nbsp; In Visual Studio, go to Tools | Code Snippet Manager&#8230;, add the directory into which you stored the snippet, and you&#8217;ll be off and running.<\/P>\n<P>The parallelfor snippet file as well as another example for parallelforeach are attached to this blog post.&nbsp; Are there any other useful snippets you&#8217;d be interested in having related to parallel programming in general or specifically to Parallel Extensions?<\/P>\n<P>Enjoy!<\/P><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/06\/72\/04\/03\/ParallelSnippets.zip\">ParallelSnippets.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduced in Visual Studio 2005, Code Snippets allow you to quickly insert reusable blocks of code into your project.&nbsp; For example, if&nbsp;you want to quickly write a for loop in C#,&nbsp;you can simply type &#8220;for&#8221; into your code file, and IntelliSense shows you the &#8220;for&#8221; code snippet: Now&nbsp;you press the tab key twice, and Visual [&hellip;]<\/p>\n","protected":false},"author":360,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7908],"tags":[7911,7909,7912],"class_list":["post-2913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pfxteam","tag-code-samples","tag-parallel-extensions","tag-task-parallel-library"],"acf":[],"blog_post_summary":"<p>Introduced in Visual Studio 2005, Code Snippets allow you to quickly insert reusable blocks of code into your project.&nbsp; For example, if&nbsp;you want to quickly write a for loop in C#,&nbsp;you can simply type &#8220;for&#8221; into your code file, and IntelliSense shows you the &#8220;for&#8221; code snippet: Now&nbsp;you press the tab key twice, and Visual [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/2913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/360"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=2913"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/2913\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=2913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=2913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=2913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}