From 86b5e6fab4f7d223d30f0b370020a6ba42cb8d05 Mon Sep 17 00:00:00 2001 From: Michał Klichowicz Date: Sun, 5 Nov 2023 14:16:25 +0100 Subject: Refactoring separate files into separate threads --- BCDD.csproj | 7 ++-- BCDD.sln | 6 ++-- Program.cs | 74 +++++++---------------------------------- src/BCDDFile.cs | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 68 deletions(-) create mode 100644 src/BCDDFile.cs diff --git a/BCDD.csproj b/BCDD.csproj index 5429313..7a77e51 100644 --- a/BCDD.csproj +++ b/BCDD.csproj @@ -1,7 +1,6 @@  - + Debug x86 @@ -53,6 +52,7 @@ + @@ -77,7 +77,6 @@ - + \ No newline at end of file diff --git a/BCDD.sln b/BCDD.sln index 93a9816..76b7a46 100644 --- a/BCDD.sln +++ b/BCDD.sln @@ -1,6 +1,8 @@  -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34221.43 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BCDD", "BCDD.csproj", "{FADD37CB-206B-4EFD-BA20-A317F1072A62}" EndProject Global diff --git a/Program.cs b/Program.cs index 51394ff..aacf8e1 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; -using System.Text; +using System.Threading; using System.Windows.Forms; using System.IO; @@ -35,72 +35,22 @@ namespace BCDD static void Main(string[] args) { List files = Program.getFiles(args); + List workers = new List(); List errors = new List(); + BCDDFile.filesCounter = files.Count; if (files.Count > 0) { foreach (String filename in files) { - try - { - Console.WriteLine("Analyzing " + filename); - PBNFile file = new PBNFile(filename); - foreach (PBNBoard board in file.Boards) - { - DDTable table = new DDTable(board); - String boardNo; - try - { - boardNo = board.GetNumber(); - } - catch (FieldNotFoundException) - { - boardNo = "?"; - } - try - { - int[,] ddTable = table.GetDDTable(); - if (ddTable != null) - { - Console.WriteLine("Board " + boardNo); - DDTable.PrintTable(ddTable); - ParScore par = new ParScore(board); - ParContract contract = par.GetParContract(ddTable); - Console.WriteLine(contract); - Console.WriteLine(); - board.SaveDDTable(ddTable); - board.SaveParContract(contract); - } - else - { - String error = "unable to determine DD table for board " + boardNo; - errors.Add(String.Format("[{0}] {1}", filename, error)); - Console.WriteLine("ERROR: " + error); - } - } - catch (DllNotFoundException) - { - throw; - } - catch (Exception e) - { - errors.Add(String.Format("[{0}:{1}] {2}", filename, boardNo, e.Message)); - Console.WriteLine("ERROR: " + e.Message); - } - file.WriteBoard(board); - } - file.Save(); - } - catch (DllNotFoundException e) - { - errors.Add("libbcalcdds.dll could not be loaded - make sure it's present in application directory!"); - Console.WriteLine("ERROR: " + e.Message); - break; - } - catch (Exception e) - { - errors.Add(e.Message); - Console.WriteLine("ERROR: " + e.Message); - } + BCDDFile worker = new BCDDFile(filename); + workers.Add(worker); + Console.WriteLine("Analyzing " + filename); + ThreadPool.QueueUserWorkItem(new WaitCallback(worker.analyze)); + } + BCDDFile.filesCountdown.WaitOne(); + foreach (BCDDFile w in workers) + { + errors.AddRange(w.errors); } if (errors.Count > 0) { Console.WriteLine("Following ERRORs occured:"); diff --git a/src/BCDDFile.cs b/src/BCDDFile.cs new file mode 100644 index 0000000..a37ead6 --- /dev/null +++ b/src/BCDDFile.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace BCDD +{ + public class BCDDFile + { + public List errors; + private String filename; + + public static int filesCounter; + public static ManualResetEvent filesCountdown = new ManualResetEvent(false); + + public BCDDFile(String filename) + { + this.errors = new List(); + this.filename = filename; + } + + private void error(String message, String boardNo = "") + { + if (!"".Equals(boardNo)) + { + message = String.Format("[{0}:{1}] {2}", this.filename, boardNo, message); + } + else + { + message = String.Format("[{0}] {1}", this.filename, message); + } + errors.Add(message); + Console.WriteLine("ERROR: " + message); + } + + public void analyze(object state) + { + try + { + PBNFile file = new PBNFile(filename); + foreach (PBNBoard board in file.Boards) + { + DDTable table = new DDTable(board); + String boardNo; + try + { + boardNo = board.GetNumber(); + } + catch (FieldNotFoundException) + { + boardNo = "?"; + } + try + { + int[,] ddTable = table.GetDDTable(); + if (ddTable != null) + { + ParScore par = new ParScore(board); + ParContract contract = par.GetParContract(ddTable); + board.SaveDDTable(ddTable); + board.SaveParContract(contract); + Console.WriteLine("Board " + boardNo); + DDTable.PrintTable(ddTable); + Console.WriteLine(contract); + Console.WriteLine(); + } + else + { + this.error("unable to determine DD table for board " + boardNo, boardNo); + } + } + catch (DllNotFoundException) + { + throw; + } + catch (Exception e) + { + this.error(e.Message, boardNo); + } + file.WriteBoard(board); + } + file.Save(); + } + catch (DllNotFoundException) + { + this.error("libbcalcdds.dll could not be loaded - make sure it's present in application directory!"); + return; + } + catch (Exception e) + { + this.error(e.Message); + } + finally + { + if (Interlocked.Decrement(ref BCDDFile.filesCounter) == 0) + { + BCDDFile.filesCountdown.Set(); + } + } + } + } +} -- cgit v1.2.3