public static ServerCommunicationStatistics TranslateDocumentsInFolderInParallel( string folderPath, string destinationFolderPath, Location location, ServerCommunicationStatistics statisticsState) { IEnumerable <Document> documentsEnumerable = GetDocumentsFromFolder(folderPath); object lockingObject = new object(); var state = statisticsState; Parallel.ForEach( documentsEnumerable, localInit: () => ServerCommunicationStatistics.Zero(), body: (document, loopState, localState) => { var result = DocumentTranslationModule.TranslateDocument( document, location, localState); WriteDocumentToDestinationFolder(result.document, destinationFolderPath); return(result.newState); }, localFinally: (localSum) => { lock (lockingObject) { state = state.Combine(localSum); } }); return(state); }
static void Main(string[] args) { var serverCommunicationStatisticsState = FolderProcessingModule.TranslateDocumentsInFolderInParallel( "c:\\inputFolder1", "c:\\outputFolder1", Location.A, ServerCommunicationStatistics.Zero()); Console.WriteLine( "Number of times communicated with server 1: " + serverCommunicationStatisticsState.NumberOfTimesCommunicatedWithServer1); Console.WriteLine( "Total time spent communicating with server 1: " + serverCommunicationStatisticsState.TotalTimeSpentCommunicatingWithServer1); Console.WriteLine( "Number of times communicated with server 2: " + serverCommunicationStatisticsState.NumberOfTimesCommunicatedWithServer2); Console.WriteLine( "Total time spent communicating with server 2: " + serverCommunicationStatisticsState.TotalTimeSpentCommunicatingWithServer2); }