public void BuildPipeline(CancellationToken token) { // create the blocks getFileUrls = new TransformManyBlock<string, string>( (url) => { List<string> fileUrls = new List<string>(); GetFileUrls(url, fileUrls); return fileUrls; } , new ExecutionDataflowBlockOptions { CancellationToken = token } ); var downloadFile = new TransformBlock<string, string>( (fileUrl) => { string fileName = Regex.Match(fileUrl, @"([^/]+$)").Value; using (var client = new WebClient()) { client.DownloadFile(fileUrl, FolderPath + "\\" + fileName); } Interlocked.Increment(ref currentFileCount); UpdateProgress((double)currentFileCount / (double)fileCount); return FolderPath + "\\" + fileName; } , new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = token, } ); var convertFile = new TransformBlock<string, string>( (fileName) => { string newFileName = ConvertedFilesFolder + @"\" + Regex.Match(fileName, @"([^\\]+$)").Value + "x"; Process process = new Process(); process.StartInfo.Arguments = string.Format(@" -nme -oice {0} {1}", fileName, newFileName); process.StartInfo.FileName = @"c:\Program Files (x86)\Microsoft Office\Office12\excelcnv.exe"; process.Start(); while (!process.WaitForExit(15000)) { process.Kill(); process.Start(); } Interlocked.Increment(ref convertedFilesCount); UpdateConversionProgress((double)convertedFilesCount / (double)fileCount); return newFileName; } , new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = token } ); var parseCitizenInfo = new ActionBlock<string>( (fileName) => { var referendumProcessor = new ReferendumProcessor(); citizensOfArmenia.Post(referendumProcessor.ProcessFile(fileName)); } , new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = token } ); // Link the blocks citizensOfArmenia = new BufferBlock<List<Citizen>>(); getFileUrls.LinkTo(downloadFile, new DataflowLinkOptions { PropagateCompletion = true }); downloadFile.LinkTo(convertFile, new DataflowLinkOptions { PropagateCompletion = true }); convertFile.LinkTo(parseCitizenInfo, new DataflowLinkOptions { PropagateCompletion = true }); parseCitizenInfo.Completion.ContinueWith((tsk) => citizensOfArmenia.Complete()); }
private void test(string folder) { string[] files = Directory.GetFiles(folder, "*.xlsx", SearchOption.TopDirectoryOnly); var parseCitizenInfo = new TransformBlock<string, List<Citizen>>( (fileName) => { var referendumProcessor = new ReferendumProcessor(); var cit = referendumProcessor.ProcessFile(fileName); return cit; } , new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount } ); foreach (string file in files) { parseCitizenInfo.Post(file); } parseCitizenInfo.Complete(); StatisticalCalculations calc = new StatisticalCalculations(); calc.FiveMostUncommonNames(parseCitizenInfo, PrintResult); calc.FiveMostCommonNames(parseCitizenInfo, PrintResult); calc.TopThreeMonth(parseCitizenInfo, PrintResult); calc.CalculateAverage(parseCitizenInfo, PrintAverage); }