using System.Threading.Tasks;
public class TaskParallelExample
public static void Main()
string[] words = CreateWordArray(@"http://www.gutenberg.org/files/2009/2009.txt");
Console.WriteLine("Begin first task...");
Console.WriteLine("Begin second task...");
GetMostCommonWords(words);
Console.WriteLine("Begin third task...");
GetCountForWord(words, "species");
Console.WriteLine("Returned from Parallel.Invoke");
Console.WriteLine("Press any key to exit");
private static string GetLongestWord(string[] words)
var longestWord = (from w in words
orderby w.Length descending
Console.WriteLine("Task 1 -- The longest word is {0}", longestWord);
private static void GetMostCommonWords(string[] words)
var frequencyOrder = (from w in words
orderby g.Count() descending
var commonWords = frequencyOrder.Take(10);
StringBuilder sb = new StringBuilder();
sb.AppendLine("Task 2 -- The most common words are:");
foreach (var v in commonWords)
Console.WriteLine(sb.ToString());
private static void GetCountForWord(string[] words, string term)
var findWord = from word in words
where word.ToUpper().Contains(term.ToUpper())
Console.WriteLine(@"Task 3 -- The word ""{0}"" occurs {1} times.", term, findWord.Count());
static string[] CreateWordArray(string uri)
Console.WriteLine("Retrieving from {0}", uri);
string s = new WebClient().DownloadString(uri);
new char[] { ' ', '\u000A', ',', '.', ';', ':', '-', '_', '/' },
StringSplitOptions.RemoveEmptyEntries);