diff --git a/RombaSharp/Partials/RombaSharp.Helpers.cs b/RombaSharp/Partials/RombaSharp.Helpers.cs index be09c22f..5faa4dc6 100644 --- a/RombaSharp/Partials/RombaSharp.Helpers.cs +++ b/RombaSharp/Partials/RombaSharp.Helpers.cs @@ -337,7 +337,7 @@ namespace RombaSharp } // Finally set all of the fields - Globals.MaxDegreeOfParallelism = workers; + Globals.MaxThreads = workers; _logdir = logdir; _tmpdir = tmpdir; _webdir = webdir; diff --git a/SabreTools.Library/Data/Build.cs b/SabreTools.Library/Data/Build.cs index 484bbaa7..9c1a49f4 100644 --- a/SabreTools.Library/Data/Build.cs +++ b/SabreTools.Library/Data/Build.cs @@ -18,6 +18,9 @@ namespace SabreTools.Library.Data /// The name to be displayed as the programB public static void Start(string name) { + // Set the maximum number of threads right off the bat + System.Threading.ThreadPool.SetMaxThreads(Globals.MaxThreads, Globals.MaxThreads); + // Dynamically create the header string, adapted from http://stackoverflow.com/questions/8200661/how-to-align-string-in-fixed-length-string int width = Console.WindowWidth - 3; string border = "+" + new string('-', width) + "+"; diff --git a/SabreTools.Library/Data/Globals.cs b/SabreTools.Library/Data/Globals.cs index 86176857..85000c9b 100644 --- a/SabreTools.Library/Data/Globals.cs +++ b/SabreTools.Library/Data/Globals.cs @@ -36,9 +36,14 @@ namespace SabreTools.Library.Data } set { _logger = value; } } - public static int MaxDegreeOfParallelism + public static int MaxThreads { - set { _maxDegreeOfParallelism = value; } + get { return _maxDegreeOfParallelism; } + set + { + _maxDegreeOfParallelism = value; + System.Threading.ThreadPool.SetMaxThreads(_maxDegreeOfParallelism, _maxDegreeOfParallelism); + } } public static ParallelOptions ParallelOptions { diff --git a/SabreTools.Library/Dats/Partials/DatFile.ConvertUpdate.cs b/SabreTools.Library/Dats/Partials/DatFile.ConvertUpdate.cs index 13aca6ca..b4f63086 100644 --- a/SabreTools.Library/Dats/Partials/DatFile.ConvertUpdate.cs +++ b/SabreTools.Library/Dats/Partials/DatFile.ConvertUpdate.cs @@ -112,7 +112,7 @@ namespace SabreTools.Library.Dats Globals.Logger.User("Processing individual DATs"); // Parse all of the DATs into their own DatFiles in the array - Parallel.For(0, inputs.Count, Globals.ParallelOptions, i => + Parallel.For(0, inputs.Count, i => { string input = inputs[i]; Globals.Logger.User("Adding DAT: " + input.Split('¬')[0]); @@ -128,11 +128,11 @@ namespace SabreTools.Library.Dats Globals.Logger.User("Processing complete in " + DateTime.Now.Subtract(start).ToString(@"hh\:mm\:ss\.fffff")); Globals.Logger.User("Populating internal DAT"); - Parallel.For(0, inputs.Count, Globals.ParallelOptions, i => + Parallel.For(0, inputs.Count, i => { // Get the list of keys from the DAT List keys = datHeaders[i].Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { // Add everything from the key to the internal DAT AddRange(key, datHeaders[i][key]); @@ -179,9 +179,7 @@ namespace SabreTools.Library.Dats Globals.Logger.User("Populating base DAT for comparison..."); List baseFileNames = FileTools.GetOnlyFilesFromInputs(basePaths); - Parallel.ForEach(baseFileNames, - Globals.ParallelOptions, - path => + Parallel.ForEach(baseFileNames, path => { Parse(path, 0, 0, keep: true, clean: clean, remUnicode: remUnicode, descAsName: descAsName); }); @@ -193,9 +191,7 @@ namespace SabreTools.Library.Dats // Now we want to compare each input DAT against the base List inputFileNames = FileTools.GetOnlyFilesFromInputs(inputPaths, appendparent: true); - Parallel.ForEach(inputFileNames, - Globals.ParallelOptions, - path => + Parallel.ForEach(inputFileNames, path => { // Get the two halves of the path string[] splitpath = path.Split('¬'); @@ -211,9 +207,7 @@ namespace SabreTools.Library.Dats // Then we do a hashwise comparison against the base DAT List keys = intDat.Keys.ToList(); - Parallel.ForEach(keys, - Globals.ParallelOptions, - key => + Parallel.ForEach(keys, key => { List datItems = intDat[key]; List keepDatItems = new List(); @@ -277,7 +271,7 @@ namespace SabreTools.Library.Dats DatFile[] outDatsArray = new DatFile[inputs.Count]; - Parallel.For(0, inputs.Count, Globals.ParallelOptions, j => + Parallel.For(0, inputs.Count, j => { string innerpost = " (" + Path.GetFileNameWithoutExtension(inputs[j].Split('¬')[0]) + " Only)"; DatFile diffData; @@ -307,7 +301,7 @@ namespace SabreTools.Library.Dats Globals.Logger.User("Populating all output DATs"); List keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { List items = DatItem.Merge(this[key]); @@ -419,7 +413,7 @@ namespace SabreTools.Library.Dats { DatFile[] outDatsArray = new DatFile[inputs.Count]; - Parallel.For(0, inputs.Count, Globals.ParallelOptions, j => + Parallel.For(0, inputs.Count, j => { string innerpost = " (" + Path.GetFileNameWithoutExtension(inputs[j].Split('¬')[0]) + " Only)"; DatFile diffData = new DatFile(this); @@ -439,7 +433,7 @@ namespace SabreTools.Library.Dats Globals.Logger.User("Populating all output DATs"); List keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { List items = DatItem.Merge(this[key]); @@ -537,7 +531,7 @@ namespace SabreTools.Library.Dats if (Type == "SuperDAT") { List keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { List items = this[key].ToList(); List newItems = new List(); @@ -586,7 +580,7 @@ namespace SabreTools.Library.Dats public void Update(List inputFileNames, string outDir, bool inplace, bool clean, bool remUnicode, bool descAsName, Filter filter, SplitType splitType, bool trim, bool single, string root) { - Parallel.ForEach(inputFileNames, Globals.ParallelOptions, inputFileName => + Parallel.ForEach(inputFileNames, inputFileName => { // Clean the input string if (inputFileName != "") @@ -624,7 +618,7 @@ namespace SabreTools.Library.Dats } List subFiles = Directory.EnumerateFiles(inputFileName, "*", SearchOption.AllDirectories).ToList(); - Parallel.ForEach(subFiles, Globals.ParallelOptions, file => + Parallel.ForEach(subFiles, file => { Globals.Logger.User("Processing \"" + Path.GetFullPath(file).Remove(0, inputFileName.Length) + "\""); DatFile innerDatdata = new DatFile(this); diff --git a/SabreTools.Library/Dats/Partials/DatFile.DFD.cs b/SabreTools.Library/Dats/Partials/DatFile.DFD.cs index 21c723b4..14db4437 100644 --- a/SabreTools.Library/Dats/Partials/DatFile.DFD.cs +++ b/SabreTools.Library/Dats/Partials/DatFile.DFD.cs @@ -66,7 +66,7 @@ namespace SabreTools.Library.Dats // Process the files in the main folder List files = Directory.EnumerateFiles(basePath, "*", SearchOption.TopDirectoryOnly).ToList(); - Parallel.ForEach(files, Globals.ParallelOptions, item => + Parallel.ForEach(files, item => { PopulateFromDirCheckFile(item, basePath, omitFromScan, bare, archivesAsFiles, enableGzip, skipFileType, addBlanks, addDate, tempDir, copyFiles, headerToCheckAgainst); @@ -74,10 +74,10 @@ namespace SabreTools.Library.Dats // Find all top-level subfolders files = Directory.EnumerateDirectories(basePath, "*", SearchOption.TopDirectoryOnly).ToList(); - Parallel.ForEach(files, Globals.ParallelOptions, item => + Parallel.ForEach(files, item => { List subfiles = Directory.EnumerateFiles(item, "*", SearchOption.AllDirectories).ToList(); - Parallel.ForEach(subfiles, Globals.ParallelOptions, subitem => + Parallel.ForEach(subfiles, subitem => { PopulateFromDirCheckFile(subitem, basePath, omitFromScan, bare, archivesAsFiles, enableGzip, skipFileType, addBlanks, addDate, tempDir, copyFiles, headerToCheckAgainst); @@ -88,7 +88,7 @@ namespace SabreTools.Library.Dats if (!Romba && addBlanks) { List empties = FileTools.GetEmptyDirectories(basePath).ToList(); - Parallel.ForEach(empties, Globals.ParallelOptions, dir => + Parallel.ForEach(empties, dir => { // Get the full path for the directory string fulldir = Path.GetFullPath(dir); @@ -243,7 +243,7 @@ namespace SabreTools.Library.Dats else { // First take care of the found items - Parallel.ForEach(extracted, Globals.ParallelOptions, rom => + Parallel.ForEach(extracted, rom => { PopulateFromDirProcessFileHelper(newItem, rom, @@ -255,7 +255,7 @@ namespace SabreTools.Library.Dats if (addBlanks) { List empties = ArchiveTools.GetEmptyFoldersInArchive(newItem); - Parallel.ForEach(empties, Globals.ParallelOptions, empty => + Parallel.ForEach(empties, empty => { Rom emptyRom = new Rom(Path.Combine(empty, "_"), newItem, omitFromScan); PopulateFromDirProcessFileHelper(newItem, diff --git a/SabreTools.Library/Dats/Partials/DatFile.Manipulate.cs b/SabreTools.Library/Dats/Partials/DatFile.Manipulate.cs index b5d03bc1..b5f3b4f1 100644 --- a/SabreTools.Library/Dats/Partials/DatFile.Manipulate.cs +++ b/SabreTools.Library/Dats/Partials/DatFile.Manipulate.cs @@ -52,7 +52,7 @@ namespace SabreTools.Library.Dats // First do the initial sort of all of the roms List keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { List roms = this[key]; @@ -143,7 +143,7 @@ namespace SabreTools.Library.Dats // Now go through and sort all of the individual lists keys = sortable.Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { // Get the possibly unsorted list List sortedlist = sortable[key]; @@ -183,7 +183,7 @@ namespace SabreTools.Library.Dats { // Loop over every key in the dictionary List keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { // For every item in the current key List items = this[key]; @@ -235,7 +235,7 @@ namespace SabreTools.Library.Dats // First we want to get a mapping for all games to description ConcurrentDictionary mapping = new ConcurrentDictionary(); List keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { List items = this[key]; foreach (DatItem item in items) @@ -250,7 +250,7 @@ namespace SabreTools.Library.Dats // Now we loop through every item and update accordingly keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { List items = this[key]; List newItems = new List(); @@ -305,7 +305,7 @@ namespace SabreTools.Library.Dats // Now process all of the roms List keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { List items = this[key]; for (int j = 0; j < items.Count; j++) diff --git a/SabreTools.Library/Dats/Partials/DatFile.Rebuild.cs b/SabreTools.Library/Dats/Partials/DatFile.Rebuild.cs index 4f230621..9f34e520 100644 --- a/SabreTools.Library/Dats/Partials/DatFile.Rebuild.cs +++ b/SabreTools.Library/Dats/Partials/DatFile.Rebuild.cs @@ -128,7 +128,7 @@ namespace SabreTools.Library.Dats // Now loop through and get only directories from the input paths List directories = new List(); - Parallel.ForEach(inputs, Globals.ParallelOptions, input => + Parallel.ForEach(inputs, input => { // Add to the list if the input is a directory if (Directory.Exists(input)) diff --git a/SabreTools.Library/Dats/Partials/DatFile.Splitters.cs b/SabreTools.Library/Dats/Partials/DatFile.Splitters.cs index 93e2bff7..43e11079 100644 --- a/SabreTools.Library/Dats/Partials/DatFile.Splitters.cs +++ b/SabreTools.Library/Dats/Partials/DatFile.Splitters.cs @@ -85,7 +85,7 @@ namespace SabreTools.Library.Dats // Now separate the roms accordingly List keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { List items = this[key]; foreach (DatItem item in items) @@ -307,7 +307,7 @@ namespace SabreTools.Library.Dats // Now populate each of the DAT objects in turn List keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { List items = this[key]; foreach (DatItem item in items) @@ -418,7 +418,7 @@ namespace SabreTools.Library.Dats keys.Sort(SplitByLevelSort); // Then, we loop over the games - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { // Here, the key is the name of the game to be used for comparison if (tempDat.Name != null && tempDat.Name != Style.GetDirectoryName(key)) @@ -584,7 +584,7 @@ namespace SabreTools.Library.Dats // Now populate each of the DAT objects in turn List keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { List items = this[key]; foreach(DatItem item in items) diff --git a/SabreTools.Library/Dats/Partials/DatFile.Statistics.cs b/SabreTools.Library/Dats/Partials/DatFile.Statistics.cs index 7b4aa5b8..e8a3d683 100644 --- a/SabreTools.Library/Dats/Partials/DatFile.Statistics.cs +++ b/SabreTools.Library/Dats/Partials/DatFile.Statistics.cs @@ -177,7 +177,7 @@ namespace SabreTools.Library.Dats // Loop through and add List keys = Keys.ToList(); - Parallel.ForEach(keys, Globals.ParallelOptions, key => + Parallel.ForEach(keys, key => { List items = this[key]; foreach(DatItem item in items) @@ -387,7 +387,7 @@ namespace SabreTools.Library.Dats // Make sure we have all files List> newinputs = new List>(); // item, basepath - Parallel.ForEach(inputs, Globals.ParallelOptions, input => + Parallel.ForEach(inputs, input => { if (File.Exists(input)) { diff --git a/SabreTools.Library/Dats/Partials/DatFile.Writers.cs b/SabreTools.Library/Dats/Partials/DatFile.Writers.cs index 4bf5d581..972bdfd4 100644 --- a/SabreTools.Library/Dats/Partials/DatFile.Writers.cs +++ b/SabreTools.Library/Dats/Partials/DatFile.Writers.cs @@ -136,7 +136,7 @@ namespace SabreTools.Library.Dats keys.Sort(new NaturalComparer()); // Write out all required formats - Parallel.ForEach(outfiles.Keys, Globals.ParallelOptions, datFormat => + Parallel.ForEach(outfiles.Keys, datFormat => { string outfile = outfiles[datFormat]; diff --git a/SabreTools.Library/External/Traverse.cs b/SabreTools.Library/External/Traverse.cs index 4d9f8332..f8580974 100644 --- a/SabreTools.Library/External/Traverse.cs +++ b/SabreTools.Library/External/Traverse.cs @@ -38,7 +38,7 @@ namespace SabreTools.Library.External } subdirs.Clear(); - Parallel.ForEach(dirs, Globals.ParallelOptions, currentDir => + Parallel.ForEach(dirs, currentDir => { string[] subDirs = Directory.GetDirectories(currentDir); @@ -54,7 +54,7 @@ namespace SabreTools.Library.External try { FileInfo[] files = dir.GetFiles("*.*", SearchOption.TopDirectoryOnly); - Parallel.ForEach(files, Globals.ParallelOptions, info => + Parallel.ForEach(files, info => { action(info); }); diff --git a/SabreTools/SabreTools.cs b/SabreTools/SabreTools.cs index 61539135..dc3cac3e 100644 --- a/SabreTools/SabreTools.cs +++ b/SabreTools/SabreTools.cs @@ -740,11 +740,11 @@ namespace SabreTools case "--mt": if (Int32.TryParse(args[++i], out int mdop)) { - Globals.MaxDegreeOfParallelism = mdop; + Globals.MaxThreads = mdop; } else { - Globals.MaxDegreeOfParallelism = 4; + Globals.MaxThreads = 4; } break; case "-n": @@ -1026,11 +1026,11 @@ namespace SabreTools case "--mt": if (Int32.TryParse(split[1], out int odop)) { - Globals.MaxDegreeOfParallelism = odop; + Globals.MaxThreads = odop; } else { - Globals.MaxDegreeOfParallelism = 4; + Globals.MaxThreads = 4; } break; case "-n":