Skip to content

Commit

Permalink
Merge branch 'main' into dim/fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
didimitrie authored Apr 3, 2021
2 parents 65fab58 + c314fc5 commit 8dc8c9a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
18 changes: 18 additions & 0 deletions GrasshopperAsyncComponent/GH_AsyncComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ protected GH_AsyncComponent(string name, string nickname, string description, st
Tasks = new List<Task>();
}

public void RequestCancellation()
{
foreach(var token in CancellationSources)
{
token.Cancel();
}

CancellationSources.Clear();
Workers.Clear();
ProgressReports.Clear();
Tasks.Clear();

Interlocked.Exchange(ref SetData, 0);

Message = "Done";
OnDisplayExpired(true);
}

public virtual void DisplayProgress(object sender, System.Timers.ElapsedEventArgs e)
{
if (Workers.Count == 0 || ProgressReports.Values.Count == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ protected override void RegisterInputParams(GH_InputParamManager pManager)
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddNumberParameter("Output", "O", "The n-th prime number.", GH_ParamAccess.item);
}

protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu)
{
base.AppendAdditionalComponentMenuItems(menu);
Menu_AppendItem(menu, "Cancel", (s, e) =>
{
this.RequestCancellation();
});
}

public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
Expand All @@ -54,7 +62,7 @@ public PrimeCalculatorWorker() : base(null) { }
public override void DoWork(Action<string, double> ReportProgress, Action Done)
{
// 👉 Checking for cancellation!
if (CancellationToken.IsCancellationRequested) return;
if (CancellationToken.IsCancellationRequested) { Done(); return; }

int count = 0;
long a = 2;
Expand All @@ -63,14 +71,14 @@ public override void DoWork(Action<string, double> ReportProgress, Action Done)
while (count < TheNthPrime)
{
// 👉 Checking for cancellation!
if (CancellationToken.IsCancellationRequested) return;
if (CancellationToken.IsCancellationRequested) { Done(); 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 (CancellationToken.IsCancellationRequested) { Done(); return; }

if (a % b == 0)
{
Expand Down Expand Up @@ -108,7 +116,7 @@ public override void GetData(IGH_DataAccess DA, GH_ComponentParamServer Params)
public override void SetData(IGH_DataAccess DA)
{
// 👉 Checking for cancellation!
if (CancellationToken.IsCancellationRequested) return;
if (CancellationToken.IsCancellationRequested) { return; }

DA.SetData(0, ThePrime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public UselessCyclesWorker() : base(null) { }
public override void DoWork(Action<string, double> ReportProgress, Action Done)
{
// Checking for cancellation
if (CancellationToken.IsCancellationRequested) return;
if (CancellationToken.IsCancellationRequested) { Done(); return; }

for (int i = 0; i <= MaxIterations; i++)
{
Expand All @@ -63,7 +63,7 @@ public override void DoWork(Action<string, double> ReportProgress, Action Done)
ReportProgress(Id, ((double)(i + 1) / (double)MaxIterations));

// Checking for cancellation
if (CancellationToken.IsCancellationRequested) return;
if (CancellationToken.IsCancellationRequested) { Done(); return; }
}

Done();
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ Q: Does this component use all my cores? A: OH YES. It goes WROOOM.

![image](https://user-images.githubusercontent.com/7696515/95597125-29310900-0a46-11eb-99ce-663b34506a7a.png)


Q: Can I enable cancellation of a longer running task?

A: Yes, now you can! In your component, just add a right click menu action like so:
Expand Down

0 comments on commit 8dc8c9a

Please sign in to comment.