static void Main(string[] args) { ServerCommunicationStatistics serverCommunicationStatisticsState = new ServerCommunicationStatistics( numberOfTimesCommunicatedWithServer1: 0, numberOfTimesCommunicatedWithServer2: 0, totalTimeSpentCommunicatingWithServer1: TimeSpan.Zero, totalTimeSpentCommunicatingWithServer2: TimeSpan.Zero); FolderProcessingModule.TranslateDocumentsInFolderInParallel( "c:\\inputFolder1", "c:\\outputFolder1", document => DocumentTranslationModule.TranslateDocument( document, paragraph => DocumentTranslationModule.TranslateParagraph( paragraph, paragraphText => DocumentTranslationModule.TranslateText( paragraphText, text => GermanTextTranslationModule.TranslateFromGerman( text, Location.A, ref serverCommunicationStatisticsState), text => SpanishTextTranslationModule.TranslateFromSpanish( text, Location.A, ref serverCommunicationStatisticsState))))); 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); }
public static Text TranslateFromGerman( Text text, Location location, ref ServerCommunicationStatistics statisticsState) { bool useServer1 = DateTime.Now.Millisecond < 500; if (useServer1) { Stopwatch stopwatch = Stopwatch.StartNew(); var result = TranslateFromGermanViaServer1(text, location); var elapsed = stopwatch.Elapsed; Utilities.UpdateViaCAS(ref statisticsState, state => state .WithTotalTimeSpentCommunicatingWithServer1( state.TotalTimeSpentCommunicatingWithServer1 + elapsed) .WithNumberOfTimesCommunicatedWithServer1( state.NumberOfTimesCommunicatedWithServer1 + 1)); return(result); } Stopwatch stopwatch2 = Stopwatch.StartNew(); var resultFromServer2 = TranslateFromGermanViaServer2(text, location); var elapsed2 = stopwatch2.Elapsed; Utilities.UpdateViaCAS(ref statisticsState, state => state .WithTotalTimeSpentCommunicatingWithServer2( state.TotalTimeSpentCommunicatingWithServer2 + elapsed2) .WithNumberOfTimesCommunicatedWithServer2( state.NumberOfTimesCommunicatedWithServer2 + 1)); return(resultFromServer2); }
public static ServerCommunicationStatistics WithNumberOfTimesCommunicatedWithServer1(this ServerCommunicationStatistics instance, Int32 newValue) { return(new ServerCommunicationStatistics(numberOfTimesCommunicatedWithServer1: newValue, numberOfTimesCommunicatedWithServer2: instance.NumberOfTimesCommunicatedWithServer2, totalTimeSpentCommunicatingWithServer1: instance.TotalTimeSpentCommunicatingWithServer1, totalTimeSpentCommunicatingWithServer2: instance.TotalTimeSpentCommunicatingWithServer2)); }