static void ApplicationFinished() { StringBuilder result = new StringBuilder(); for (int i = 0; i < App.Threads.Count; i++) { PiCalculatorThread pcgt = (PiCalculatorThread)App.Threads[i]; result.Append(pcgt.Result); } Console.WriteLine( "===\nThe value of Pi to {0} digits is:\n3.{1}\n===\nTotal time taken = {2}\n===", NumberOfDigits, result, DateTime.Now - StartTime); //Console.WriteLine("Thread finished fired: " + th + " times"); Console.WriteLine("Application Finished"); }
static void Main() { Console.WriteLine("[Pi Calculator Grid Application]\n--------------------------------\n"); Console.WriteLine("Press <enter> to start ..."); Console.ReadLine(); Logger.LogHandler += new LogEventHandler(LogHandler); try { // get the number of digits from the user bool numberOfDigitsEntered = false; while (!numberOfDigitsEntered) { try { NumberOfDigits = Int32.Parse(Utils.ValueFromConsole("Digits to calculate", "100")); if (NumberOfDigits > 0) { numberOfDigitsEntered = true; } } catch (Exception) { Console.WriteLine("Invalid numeric value."); numberOfDigitsEntered = false; } } // get settings from user GConnection gc = GConnection.FromConsole("localhost", "9000", "user", "user"); StartTiming(); // create a new grid application App = new GApplication(gc); App.ApplicationName = "PI Calculator - Alchemi sample"; // add the module containing PiCalculatorThread to the application manifest App.Manifest.Add(new ModuleDependency(typeof(PiCalculatorThread).Module)); NumThreads = (Int32)Math.Floor((double)NumberOfDigits / DigitsPerThread); if (DigitsPerThread * NumThreads < NumberOfDigits) { NumThreads++; } // create and add the required number of grid threads for (int i = 0; i < NumThreads; i++) { int StartDigitNum = 1 + (i * DigitsPerThread); /// the number of digits for each thread /// Each thread will get DigitsPerThread digits except the last one /// which might get less int DigitsForThisThread = Math.Min(DigitsPerThread, NumberOfDigits - i * DigitsPerThread); Console.WriteLine( "starting a thread to calculate the digits of pi from {0} to {1}", StartDigitNum, StartDigitNum + DigitsForThisThread - 1); PiCalculatorThread thread = new PiCalculatorThread( StartDigitNum, DigitsForThisThread ); App.Threads.Add(thread); } // subcribe to events App.ThreadFinish += new GThreadFinish(ThreadFinished); App.ApplicationFinish += new GApplicationFinish(ApplicationFinished); // start the grid application App.Start(); logger.Debug("PiCalc started."); } catch (Exception e) { Console.WriteLine("ERROR: {0}", e.ToString()); } Console.ReadLine(); }