diff options
author | chriszimmerman <cjay.zimmerman@gmail.com> | 2016-10-11 03:33:17 -0400 |
---|---|---|
committer | ven <vendethiel@hotmail.fr> | 2016-10-11 09:33:17 +0200 |
commit | 7b96973204f7309b5a42be10e4e24a0bb005ba85 (patch) | |
tree | 77f9dc6e63b4ce37017d3dba10d0045ba192c901 /csharp.html.markdown | |
parent | c8fa53ca1fcb9445bd136390c8016bba879eaa92 (diff) |
Csharp parallel (#2443)
* [csharp/en] - Attempts to address issue #1064 by using a different parallel example.
Diffstat (limited to 'csharp.html.markdown')
-rw-r--r-- | csharp.html.markdown | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/csharp.html.markdown b/csharp.html.markdown index a3d93a0b..2cacfc00 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -547,28 +547,22 @@ on a new line! ""Wow!"", the masses cried"; // PARALLEL FRAMEWORK // http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx - var websites = new string[] { - "http://www.google.com", "http://www.reddit.com", - "http://www.shaunmccarthy.com" - }; - var responses = new Dictionary<string, string>(); - // Will spin up separate threads for each request, and join on them - // before going to the next step! - Parallel.ForEach(websites, - new ParallelOptions() {MaxDegreeOfParallelism = 3}, // max of 3 threads - website => - { - // Do something that takes a long time on the file - using (var r = WebRequest.Create(new Uri(website)).GetResponse()) + var words = new List<string> {"dog", "cat", "horse", "pony"}; + + Parallel.ForEach(words, + new ParallelOptions() { MaxDegreeOfParallelism = 4 }, + word => { - responses[website] = r.ContentType; + Console.WriteLine(word); } - }); + ); - // This won't happen till after all requests have been completed - foreach (var key in responses.Keys) - Console.WriteLine("{0}:{1}", key, responses[key]); + //Running this will produce different outputs + //since each thread finishes at different times. + //Some example outputs are: + //cat dog horse pony + //dog horse pony cat // DYNAMIC OBJECTS (great for working with other languages) dynamic student = new ExpandoObject(); |