refactor(cleanup): cleans up a bit, adds some comments, renames components

This commit is contained in:
Dimitrie Stefanescu
2020-10-07 19:35:43 +01:00
parent fd229e1a7f
commit 1446fe2e8a
4 changed files with 96 additions and 52 deletions
@@ -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);
}
}
}
@@ -64,6 +64,7 @@
<Compile Include="Base\IAsyncComponentWorker.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SampleImplementations\Sample_PrimeCalculatorAsyncComponent.cs" />
<Compile Include="SampleImplementations\Sample_UslessCyclesComponent.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
@@ -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<string> ReportProgress, Action<string, GH_RuntimeMessageLevel> 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<string> ReportProgress, Action<string, GH_RuntimeMessageLevel> 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}");
}
}
@@ -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<string> ReportProgress, Action<string, GH_RuntimeMessageLevel> 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.");
}
}
}