summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichał Klichowicz <emkael@tlen.pl>2023-11-05 14:16:25 +0100
committerMichał Klichowicz <emkael@tlen.pl>2023-11-05 14:16:25 +0100
commit86b5e6fab4f7d223d30f0b370020a6ba42cb8d05 (patch)
tree9c6a84ebe11e7ddff8e06bd665f3146821091f64
parentbc32d973874ff5cd1d495395f9f8489e95b9bb0f (diff)
Refactoring separate files into separate threads
-rw-r--r--BCDD.csproj7
-rw-r--r--BCDD.sln6
-rw-r--r--Program.cs74
-rw-r--r--src/BCDDFile.cs101
4 files changed, 120 insertions, 68 deletions
diff --git a/BCDD.csproj b/BCDD.csproj
index 5429313..7a77e51 100644
--- a/BCDD.csproj
+++ b/BCDD.csproj
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
- <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"
- Condition="Exists('$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets')" />
+ <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" Condition="Exists('$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
@@ -53,6 +52,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="src\BCalcWrapper.cs" />
+ <Compile Include="src\BCDDFile.cs" />
<Compile Include="src\DDTable.cs" />
<Compile Include="src\ParContract.cs" />
<Compile Include="src\ParScore.cs" />
@@ -77,7 +77,6 @@
<GetAssemblyIdentity AssemblyFiles="$(OutputPath)\$(SolutionName).exe">
<Output TaskParameter="Assemblies" ItemName="assemblyInfo" />
</GetAssemblyIdentity>
- <Zip Files="@(BundleFiles)" WorkingDirectory="bundle\tmp\" Flatten="true"
- ZipFileName="bundle\$(SolutionName)-$([System.Version]::Parse(%(assemblyInfo.Version)).ToString(2)).zip" />
+ <Zip Files="@(BundleFiles)" WorkingDirectory="bundle\tmp\" Flatten="true" ZipFileName="bundle\$(SolutionName)-$([System.Version]::Parse(%(assemblyInfo.Version)).ToString(2)).zip" />
</Target>
</Project> \ 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<String> files = Program.getFiles(args);
+ List<BCDDFile> workers = new List<BCDDFile>();
List<String> errors = new List<String>();
+ 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<String> errors;
+ private String filename;
+
+ public static int filesCounter;
+ public static ManualResetEvent filesCountdown = new ManualResetEvent(false);
+
+ public BCDDFile(String filename)
+ {
+ this.errors = new List<String>();
+ 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();
+ }
+ }
+ }
+ }
+}