From 1446fe2e8aafae4bb360fa6b62cb8d1b59e4fcca Mon Sep 17 00:00:00 2001 From: Dimitrie Stefanescu Date: Wed, 7 Oct 2020 19:35:43 +0100 Subject: [PATCH] refactor(cleanup): cleans up a bit, adds some comments, renames components --- .../Base/GH_AsyncComponent.cs | 6 +- .../GrasshopperAsyncComponent.csproj | 1 + .../Sample_PrimeCalculatorAsyncComponent.cs | 61 +++----------- .../Sample_UslessCyclesComponent.cs | 80 +++++++++++++++++++ 4 files changed, 96 insertions(+), 52 deletions(-) create mode 100644 GrasshopperAsyncComponent/SampleImplementations/Sample_UslessCyclesComponent.cs diff --git a/GrasshopperAsyncComponent/Base/GH_AsyncComponent.cs b/GrasshopperAsyncComponent/Base/GH_AsyncComponent.cs index f955ce9..723a1a5 100644 --- a/GrasshopperAsyncComponent/Base/GH_AsyncComponent.cs +++ b/GrasshopperAsyncComponent/Base/GH_AsyncComponent.cs @@ -78,7 +78,9 @@ namespace GrasshopperAsyncComponent if (State == Workers.Count) { SetData = true; + // We need to reverse the workers list to set the outputs in the same order as the inputs. Workers.Reverse(); + Rhino.RhinoApp.InvokeOnUiThread((Action)delegate { ExpireSolution(true); @@ -109,6 +111,7 @@ namespace GrasshopperAsyncComponent protected override void AfterSolveInstance() { + // We need to start all the tasks as close as possible to each other. if (State == 0 && Tasks.Count > 0) { foreach (var task in Tasks) task.Start(); @@ -180,8 +183,7 @@ namespace GrasshopperAsyncComponent SetData = false; Message = "Done"; - //OnDisplayExpired(true); - OnDisplayExpired(false); + OnDisplayExpired(true); } } } diff --git a/GrasshopperAsyncComponent/GrasshopperAsyncComponent.csproj b/GrasshopperAsyncComponent/GrasshopperAsyncComponent.csproj index 8a12863..1e44e20 100644 --- a/GrasshopperAsyncComponent/GrasshopperAsyncComponent.csproj +++ b/GrasshopperAsyncComponent/GrasshopperAsyncComponent.csproj @@ -64,6 +64,7 @@ + diff --git a/GrasshopperAsyncComponent/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs b/GrasshopperAsyncComponent/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs index 35b1479..acadebc 100644 --- a/GrasshopperAsyncComponent/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs +++ b/GrasshopperAsyncComponent/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs @@ -10,7 +10,7 @@ namespace GrasshopperAsyncComponent.SampleImplementations { public class Sample_PrimeCalculatorAsyncComponent : GH_AsyncComponent { - public override Guid ComponentGuid { get => new Guid("DF2B93E2-052D-4BE4-BC62-90DC1F169BF6"); } + public override Guid ComponentGuid { get => new Guid("22C612B0-2C57-47CE-B9FE-E10621F18933"); } protected override System.Drawing.Bitmap Icon { get => null; } @@ -32,71 +32,30 @@ namespace GrasshopperAsyncComponent.SampleImplementations } } - public class SampleAsyncWorker : WorkerInstance - { - int MaxIterations { get; set; } = 100; - - public override void DoWork(Action ReportProgress, Action ReportError, Action Done) - { - if (CancellationToken.IsCancellationRequested) return; - - for (int i = 0; i <= MaxIterations; i++) - { - var sw = new SpinWait(); - for (int j = 0; j <= 100; j++) - sw.SpinOnce(); - - ReportProgress(((double)(i + 1) / (double)MaxIterations).ToString("0.00%")); - - if (CancellationToken.IsCancellationRequested) return; - } - - Done(); - } - - public override WorkerInstance Duplicate() => new SampleAsyncWorker(); - - public override void GetData(IGH_DataAccess DA, GH_ComponentParamServer Params) - { - if (CancellationToken.IsCancellationRequested) return; - - int _maxIterations = 100; - DA.GetData(0, ref _maxIterations); - if (_maxIterations > 1000) _maxIterations = 1000; - if (_maxIterations < 10) _maxIterations = 10; - - MaxIterations = _maxIterations; - } - - public override void SetData(IGH_DataAccess DA) - { - if (CancellationToken.IsCancellationRequested) return; - DA.SetData(0, $"Hello world. Worker {Id} has spun for {MaxIterations} iterations."); - } - } - public class PrimeCalculatorWorker : WorkerInstance { - int TehNthPrime { get; set; } = 100; + int TheNthPrime { get; set; } = 100; long ThePrime { get; set; } = -1; public override void DoWork(Action ReportProgress, Action ReportError, Action Done) { + // πŸ‘‰ Checking for cancellation! if (CancellationToken.IsCancellationRequested) return; int count = 0; long a = 2; // Thanks Steak Overflow (TM) https://stackoverflow.com/a/13001749/ - while (count < TehNthPrime) + while (count < TheNthPrime) { + // πŸ‘‰ Checking for cancellation! if (CancellationToken.IsCancellationRequested) return; long b = 2; int prime = 1;// to check if found a prime while (b * b <= a) { - + // πŸ‘‰ Checking for cancellation! if (CancellationToken.IsCancellationRequested) return; if (a % b == 0) @@ -107,7 +66,7 @@ namespace GrasshopperAsyncComponent.SampleImplementations b++; } - ReportProgress(((double)(count) / (double)TehNthPrime).ToString("0.00%")); + ReportProgress(((double)(count) / TheNthPrime).ToString("0.00%")); if (prime > 0) { @@ -129,13 +88,15 @@ namespace GrasshopperAsyncComponent.SampleImplementations if (_nthPrime > 1000000) _nthPrime = 1000000; if (_nthPrime < 1) _nthPrime = 1; - TehNthPrime = _nthPrime; + TheNthPrime = _nthPrime; } public override void SetData(IGH_DataAccess DA) { + // πŸ‘‰ Checking for cancellation! if (CancellationToken.IsCancellationRequested) return; - DA.SetData(0, $"W_ID {Id}: {TehNthPrime}th prime is: {ThePrime}"); + + DA.SetData(0, $"Worker {Id}: {TheNthPrime}th prime is: {ThePrime}"); } } diff --git a/GrasshopperAsyncComponent/SampleImplementations/Sample_UslessCyclesComponent.cs b/GrasshopperAsyncComponent/SampleImplementations/Sample_UslessCyclesComponent.cs new file mode 100644 index 0000000..dcb6041 --- /dev/null +++ b/GrasshopperAsyncComponent/SampleImplementations/Sample_UslessCyclesComponent.cs @@ -0,0 +1,80 @@ +ο»Ώusing Grasshopper.Kernel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace GrasshopperAsyncComponent.SampleImplementations +{ + public class Sample_UselessCyclesAsyncComponent : GH_AsyncComponent + { + public override Guid ComponentGuid { get => new Guid("DF2B93E2-052D-4BE4-BC62-90DC1F169BF6"); } + + protected override System.Drawing.Bitmap Icon { get => null; } + + public override GH_Exposure Exposure => GH_Exposure.primary; + + public Sample_UselessCyclesAsyncComponent() : base("Sample Async Component", "CYCLOMATRON-X", "Spins uselessly.", "Samples", "Async") + { + BaseWorker = new UselessCyclesWorker(); + } + + protected override void RegisterInputParams(GH_InputParamManager pManager) + { + pManager.AddIntegerParameter("N", "N", "Number of spins.", GH_ParamAccess.item); + } + + protected override void RegisterOutputParams(GH_OutputParamManager pManager) + { + pManager.AddTextParameter("Output", "O", "Nothing really interesting.", GH_ParamAccess.item); + } + } + + public class UselessCyclesWorker : WorkerInstance + { + int MaxIterations { get; set; } = 100; + + public override void DoWork(Action ReportProgress, Action ReportError, Action Done) + { + // Checking for cancellation + if (CancellationToken.IsCancellationRequested) return; + + for (int i = 0; i <= MaxIterations; i++) + { + var sw = new SpinWait(); + for (int j = 0; j <= 100; j++) + sw.SpinOnce(); + + ReportProgress(((double)(i + 1) / (double)MaxIterations).ToString("0.00%")); + + // Checking for cancellation + if (CancellationToken.IsCancellationRequested) return; + } + + Done(); + } + + public override WorkerInstance Duplicate() => new UselessCyclesWorker(); + + public override void GetData(IGH_DataAccess DA, GH_ComponentParamServer Params) + { + if (CancellationToken.IsCancellationRequested) return; + + int _maxIterations = 100; + DA.GetData(0, ref _maxIterations); + if (_maxIterations > 1000) _maxIterations = 1000; + if (_maxIterations < 10) _maxIterations = 10; + + MaxIterations = _maxIterations; + } + + public override void SetData(IGH_DataAccess DA) + { + if (CancellationToken.IsCancellationRequested) return; + DA.SetData(0, $"Hello world. Worker {Id} has spun for {MaxIterations} iterations."); + } + } + +}