static void Main(string[] args) { var wordPattern = new Regex(@"\w+"); var logger = new FileLogger(); var words = new Dictionary <string, int>(StringComparer.CurrentCultureIgnoreCase); logger.LogMessage("Starting Async/Await test"); var logSempahore = new Semaphore(1, 1); var getSemaphore = new Semaphore(5, 5); var bowSemaphore = new Semaphore(1, 1); var books = new List <string[]>(); logger.LogMessage("Starting Download"); Parallel.ForEach(Info.Books, x => { getSemaphore.WaitOne(); var book = Utils.GetFile(Utils.GetUrl(x.Id)); getSemaphore.Release(); if (book.Length > 0) { logSempahore.WaitOne(); logger.LogMessage($"Finished Download{x.Title}"); logSempahore.Release(); logSempahore.WaitOne(); logger.LogMessage($"Saving {x.Title}"); logSempahore.Release(); Utils.SaveFile(x.Title, book); logSempahore.WaitOne(); logger.LogMessage($"Suceesfully saved {x.Title}"); logSempahore.Release(); bowSemaphore.WaitOne(); foreach (Match match in wordPattern.Matches(book)) { var currentCount = 0; var word = Regex.Replace(match.Value, @"[^0 - 9A - Za - z._\s]", ""); words.TryGetValue(match.Value, out currentCount); currentCount++; words[match.Value] = currentCount; } bowSemaphore.Release(); } }); logger.LogMessage("Started to log BoW"); logger.LogBoW(words); logger.LogMessage("Finished to log BoW"); Console.WriteLine("Press any key to finish"); _ = Console.ReadKey(); }
static void Main(string[] args) { var logger = new FileLogger(); string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Books"); logger.LogMessage("Starting Async/Await test"); createFolder(path); List <Task> tasks = new List <Task>(); foreach (var book in Info.Books) { tasks.Add(Task.Factory.StartNew(async() => { logger.LogMessage("Starting downloading book :" + book.Id + " " + book.Title); var bookFile = await Utils.GetFile(Utils.GetUrl(book.Id)); logger.LogMessage("Finishing downloading book :" + book.Id + " " + book.Title); return(bookFile); }) .ContinueWith((bookFile) => { logger.LogMessage("Starting saving book :" + book.Id + " " + book.Title); Utils.SaveFile(Path.Combine(path, book.Title + ".txt"), bookFile.Result.Result); logger.LogMessage("Finishing saving book :" + book.Id + " " + book.Title); return(bookFile); }) .ContinueWith((bookFile) => { logger.LogMessage("Starting makeing the bag of words :" + book.Id + " " + book.Title); BagOfWords(bookFile.Result.Result.Result); logger.LogMessage("Finishing makeing the bag of words:" + book.Id + " " + book.Title); })); } Task.WhenAll(tasks).ContinueWith((x) => { logger.LogMessage("Counting words in the bag: "); Console.WriteLine("Counting words in the bag: "); foreach (var word in bagOfWords) { Console.WriteLine($"The word: {word.Key} appears {word.Value} times"); logger.LogMessage($"The word: {word.Key} appears {word.Value} times"); } ; Console.WriteLine("Press any key to finish"); }); // Code here Console.WriteLine("Wait I am processing the data"); _ = Console.ReadKey(); }
static void Main(string[] args) { var logger = new FileLogger(); logger.LogMessage("Starting Async/Await test"); var path = "C:/Users/ramiro.flores/Desktop/Books/"; var downloadTasks = new List<Task>(); var bows = new List<Dictionary<string, int>>(); Info.Books.ForEach((x) => downloadTasks.Add(new Task(() => Utils.SaveFile(path + $"{x.Title}.txt", Utils.GetFile(Utils.GetUrl(x.Id))) ))); downloadTasks.ForEach(x => x.Start()); Task.WaitAll(downloadTasks.ToArray()); var directory = new DirectoryInfo(path); var bookToBoowTasks = new List<Task>(); Info.Books.ForEach((x) => bookToBoowTasks.Add(new Task( () => { //create the bow var text = File.ReadAllText(path + $"{x.Title}.txt"); var words = Regex.Matches(text, @"\w+").Cast<Match>().Where(m => m.Success).Select(m => m.Value); var bow = words.GroupBy(w => w).ToDictionary(g => g.Key, g => g.Count()); bows.Add(bow); } ) )); bookToBoowTasks.ForEach(x => x.Start()); Task.WaitAll(bookToBoowTasks.ToArray()); bows.ForEach(x => { foreach (KeyValuePair<string, int> kvp in x) { logger.LogMessage($"Key = {kvp.Key}, Value = {kvp.Value}"); } }); Console.WriteLine("Press any key to finish"); _ = Console.ReadKey(); }