/// <summary> /// The function that each thread will run. Will process each addon it has been provided. /// </summary> /// <param name="infoObject">Should be an instance of ProcessingThreadInfo, contains the information the thread needs to execute with.</param> private void ProcessThread(object infoObject) { // Convert object to the correct type. ProcessingThreadInfo info = (ProcessingThreadInfo)infoObject; // Locals. Worker instance = info.instance; int processingThreadNum = info.processingThreadNum; string[] addons = info.addonPaths; string outputDirectory = info.outputDirectoryPath; string workerThreadString = $"[Worker Thread {processingThreadNum}]"; // Process the addon folders. Console.WriteLine("{0} Started processing...", workerThreadString); Directory.CreateDirectory(outputDirectory); for (int i = 0; i < addons.Length; i++) { string addonPath = addons[i]; Console.WriteLine("{0} Started processing addon: {1}", workerThreadString, addonPath); ProcessDirectory(instance, outputDirectory, addonPath); Console.WriteLine("{0} Finished processing addon: {1}", workerThreadString, addonPath); } Console.WriteLine("{0} Finished merging {1} addons into {2}", workerThreadString, addons.Length, outputDirectory); // Write logs to temp files. if (instance.options.canLog) { Console.WriteLine("{0} Writing logs to file...", workerThreadString); // Format paths string errorPath = String.Format(logLocation, "errors", processingThreadNum); string conflictPath = String.Format(logLocation, "conflicts", processingThreadNum); // Write errors if (instance.errors.Count != 0) { File.WriteAllLines(errorPath, instance.errors.ToArray()); } else { File.WriteAllLines(errorPath, new string[] { "No Errors!" }); } // Write conflicts if (instance.conflicts.Count != 0) { File.WriteAllLines(conflictPath, instance.conflicts.ToArray()); } else { File.WriteAllLines(conflictPath, new string[] { "No Conflicts!" }); } } Console.WriteLine("{0} Worker finished.", workerThreadString); }
/// <summary> /// Creates a new instance of Worker. This is used to handle a processing thread. /// </summary> /// <param name="workerNum">The ID of the worker.</param> /// <param name="outputDirectory">The directory the thread should be outputting to.</param> /// <param name="addons">The array of addon directories the thread will be working with.</param> /// <param name="options">Struct of configurable options for the thread.</param> public Worker(int workerNum, string outputDirectory, string[] addons, WorkerOptions options) { // Create the info struct. info = new ProcessingThreadInfo() { instance = this, processingThreadNum = workerNum, outputDirectoryPath = outputDirectory, addonPaths = addons }; // Assign the options. this.options = options; // Create the worker thread. workerThread = new Thread(new ParameterizedThreadStart(ProcessThread)); // Instantiate list objects if we are logging. if (options.canLog) { errors = new List <string>(); conflicts = new List <string>(); } }