static void Main(string[] args) { ServerCommunicationStatistics serverCommunicationStatisticsState = new ServerCommunicationStatistics( numberOfTimesCommunicatedWithServer1: 0, numberOfTimesCommunicatedWithServer2: 0, totalTimeSpentCommunicatingWithServer1: TimeSpan.Zero, totalTimeSpentCommunicatingWithServer2: TimeSpan.Zero); object serverCommunicationStatisticsStateLockingObject = new object(); 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, serverCommunicationStatisticsStateLockingObject), text => SpanishTextTranslationModule.TranslateFromSpanish( text, Location.A, ref serverCommunicationStatisticsState, serverCommunicationStatisticsStateLockingObject))))); 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 TranslateFromSpanish( Text text, Location location, ref ServerCommunicationStatistics statisticsState, object statisticsStateLockingObject) { bool useServer1 = DateTime.Now.Millisecond < 500; if (useServer1) { Stopwatch stopwatch = Stopwatch.StartNew(); var result = TranslateFromSpanishViaServer1(text, location); var elapsed = stopwatch.Elapsed; lock (statisticsStateLockingObject) { statisticsState = statisticsState .WithTotalTimeSpentCommunicatingWithServer1( statisticsState.TotalTimeSpentCommunicatingWithServer1 + elapsed) .WithNumberOfTimesCommunicatedWithServer1( statisticsState.NumberOfTimesCommunicatedWithServer1 + 1); } return(result); } Stopwatch stopwatch2 = Stopwatch.StartNew(); var resultFromServer2 = TranslateFromSpanishViaServer2(text, location); var elapsed2 = stopwatch2.Elapsed; lock (statisticsStateLockingObject) { statisticsState = statisticsState .WithTotalTimeSpentCommunicatingWithServer2( statisticsState.TotalTimeSpentCommunicatingWithServer2 + elapsed2) .WithNumberOfTimesCommunicatedWithServer2( statisticsState.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)); }