/// <summary> /// The GApplication.ThreadFinished event fires each time a thread has finished processing. /// </summary> /// <param name="thread">The thread that finished.</param> private void gridApp_ThreadFinish(GThread thread) { // Cast the GThread to our custom type so that we can access our custom properties. CustomGThread customGThread = (CustomGThread)thread; Console.WriteLine("Thread " + customGThread.Index + " finished"); // Add the output from the thread to our output array. this._outputArray[customGThread.Index] = customGThread.Output; }
void Run() { // All grid applications must have a GApplication instance. The GConnection information // specifies the Manager's host name, the port to connect on, and the username and password // to use. A GApplication is created with this connection information. GConnection gridConnection = new GConnection("localhost", 9000, "user", "user"); GApplication gridApplication = new GApplication(gridConnection); while (!File.Exists(this._inputFileName)) { if (this._inputFileName != null) { Console.WriteLine("\nFile does not exist. Please try again.\n"); } Console.WriteLine("Enter the input file name: "); this._inputFileName = Console.ReadLine(); } Console.WriteLine("Enter the output file name: "); this._outputFileName = Console.ReadLine(); try { // Open the input file for reading. StreamReader streamReader = new StreamReader(this._inputFileName); int blockSize = 100; char[] charBuffer = new char[blockSize]; string stringBuffer; int currentBlock = 0; // In order to ensure that all required DLLs to run the thread exist at each Executor // we must add a dependency to the Manifest for each DLL and/or file needed by the GThreads. gridApplication.Manifest.Add(new ModuleDependency(typeof(CustomGThread).Module)); // Loop through the file reading blocks of size blockSize into the char[] array charBuffer // until we hit the end of the file. while (streamReader.ReadBlock(charBuffer, 0, blockSize) != 0) { // ReadBlock returns a char[] buffer so convert it to a string to pass to our custom GThread. stringBuffer = new string( charBuffer ); // Instantiate our custom GThread and pass the data we need to process through the constructor. // Note: other methods of passing data to GThreads are possible. CustomGThread customGThread = new CustomGThread(currentBlock, stringBuffer); // Add the thread to the GApplication's Threads collection. gridApplication.Threads.Add(customGThread); currentBlock++; } // Close the input file. streamReader.Close(); // Resize the output array to hold one entry per GThread. this._outputArray = new string[currentBlock]; // Bind the ThreadFinished and ApplicationFinished events to local event handlers. gridApplication.ThreadFinish += new GThreadFinish(gridApp_ThreadFinish); gridApplication.ApplicationFinish += new GApplicationFinish(gridApp_ApplicationFinish); // Start the GApplication. gridApplication.Start(); Console.WriteLine("Application Started"); } catch (Exception ex) { Console.WriteLine("An exception occurred: " + ex.Message + "\r\n" + ex.StackTrace); } }