{"id":55919,"date":"2010-04-15T09:00:00","date_gmt":"2010-04-15T09:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/pfxteam\/2010\/04\/15\/parallelextensionsextras-tour-11-paralleldynamicinvoke\/"},"modified":"2010-04-15T09:00:00","modified_gmt":"2010-04-15T09:00:00","slug":"parallelextensionsextras-tour-11-paralleldynamicinvoke","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/parallelextensionsextras-tour-11-paralleldynamicinvoke\/","title":{"rendered":"ParallelExtensionsExtras Tour &#8211; #11 &#8211; ParallelDynamicInvoke"},"content":{"rendered":"<p><font size=\"3\"><font face=\"Calibri\"><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><em>(The full set of ParallelExtensionsExtras Tour posts is available&nbsp;<\/em><a href=\"https:\/\/blogs.msdn.com\/pfxteam\/archive\/2010\/04\/04\/9990342.aspx\"><font color=\"#dd4a21\"><em>here<\/em><\/font><\/a><em>.)<\/em>&nbsp;<\/font><\/font><\/p>\n<p class=\"MsoNormal\">Delegates in .NET may have one or more methods in their invocation list.<span>&nbsp; <\/span>When you invoke a delegate, such as through the Delegate.DynamicInvoke method, the net result is that all of the methods in the invocation list get invoked, one after the other.<span>&nbsp; <\/span>Of course, in a parallel world we might want to invoke all of the methods in the invocation list in parallel instead of sequentially. The DelegateExtensions.cs file in <a href=\"https:\/\/code.msdn.microsoft.com\/ParExtSamples\">ParallelExtensionsExtras<\/a> provides an extension method on Delegate that does just that. <\/p>\n<p><\/font><\/p>\n<p class=\"Code\"><font face=\"Consolas\">public static object ParallelDynamicInvoke(<\/p>\n<p><\/font><\/p>\n<p class=\"Code\"><font face=\"Consolas\"><span>&nbsp;&nbsp;&nbsp; <\/span>this Delegate multicastDelegate, params object[] args)<\/p>\n<p><\/font><\/p>\n<p class=\"Code\"><font face=\"Consolas\">{<\/p>\n<p><\/font><\/p>\n<p class=\"Code\"><font face=\"Consolas\"><span>&nbsp;&nbsp;&nbsp; <\/span>return multicastDelegate.GetInvocationList()<\/p>\n<p><\/font><\/p>\n<p class=\"Code\"><font face=\"Consolas\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>.AsParallel().AsOrdered()<\/p>\n<p><\/font><\/p>\n<p class=\"Code\"><font face=\"Consolas\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>.Select(d =&gt; d.DynamicInvoke(args))<\/p>\n<p><\/font><\/p>\n<p class=\"Code\"><font face=\"Consolas\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>.Last();<\/p>\n<p><\/font><\/p>\n<p class=\"Code\"><font face=\"Consolas\">}<\/p>\n<p><\/font><\/p>\n<p class=\"Code\">\n<p><font face=\"Consolas\">&nbsp;<\/font><\/p>\n<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">This ParallelDynamicInvoke extension method accepts the target multicast delegate as well as the params array to provide to the delegate invocation, just as with Delegate.DynamicInvoke:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"Code\"><font face=\"Consolas\">public object DynamicInvoke(params object[] args);<\/p>\n<p><\/font><\/p>\n<p class=\"Code\">\n<p><font face=\"Consolas\">&nbsp;<\/font><\/p>\n<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">ParallelDynamicInvoke uses the delegate&rsquo;s GetInvocationList method to retrieve an array of all of the methods in the invocation list, each of which is represented as a Delegate.<span>&nbsp; <\/span>Then, PLINQ is used to invoke each individual delegate, returning the last delegate&rsquo;s result.<\/p>\n<p><\/font><\/font><\/p>\n<p><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>(The full set of ParallelExtensionsExtras Tour posts is available&nbsp;here.)&nbsp; Delegates in .NET may have one or more methods in their invocation list.&nbsp; When you invoke a delegate, such as through the Delegate.DynamicInvoke method, the net result is that all of the methods in the invocation list get invoked, one after the other.&nbsp; Of course, in [&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":[7907,7911,7909,7924,7910],"class_list":["post-55919","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pfxteam","tag-net-4","tag-code-samples","tag-parallel-extensions","tag-parallelextensionsextras","tag-plinq"],"acf":[],"blog_post_summary":"<p>(The full set of ParallelExtensionsExtras Tour posts is available&nbsp;here.)&nbsp; Delegates in .NET may have one or more methods in their invocation list.&nbsp; When you invoke a delegate, such as through the Delegate.DynamicInvoke method, the net result is that all of the methods in the invocation list get invoked, one after the other.&nbsp; Of course, in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/55919","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=55919"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/55919\/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=55919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=55919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=55919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}