diff --git a/GrasshopperAsyncComponent/GH_AsyncComponent.cs b/GrasshopperAsyncComponent/GH_AsyncComponent.cs index 3178cd3..d32e72f 100755 --- a/GrasshopperAsyncComponent/GH_AsyncComponent.cs +++ b/GrasshopperAsyncComponent/GH_AsyncComponent.cs @@ -86,6 +86,24 @@ namespace GrasshopperAsyncComponent Tasks = new List(); } + 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) diff --git a/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs b/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs index 22b4e0b..105cc9c 100755 --- a/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs +++ b/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs @@ -31,7 +31,15 @@ namespace GrasshopperAsyncComponentDemo.SampleImplementations 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) @@ -54,7 +62,7 @@ namespace GrasshopperAsyncComponentDemo.SampleImplementations public override void DoWork(Action ReportProgress, Action Done) { // 👉 Checking for cancellation! - if (CancellationToken.IsCancellationRequested) return; + if (CancellationToken.IsCancellationRequested) { Done(); return; } int count = 0; long a = 2; @@ -63,14 +71,14 @@ namespace GrasshopperAsyncComponentDemo.SampleImplementations 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) { @@ -108,7 +116,7 @@ namespace GrasshopperAsyncComponentDemo.SampleImplementations public override void SetData(IGH_DataAccess DA) { // 👉 Checking for cancellation! - if (CancellationToken.IsCancellationRequested) return; + if (CancellationToken.IsCancellationRequested) { return; } DA.SetData(0, ThePrime); } diff --git a/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_UslessCyclesComponent.cs b/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_UslessCyclesComponent.cs index b0027f8..498b144 100755 --- a/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_UslessCyclesComponent.cs +++ b/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_UslessCyclesComponent.cs @@ -52,7 +52,7 @@ namespace GrasshopperAsyncComponentDemo.SampleImplementations public override void DoWork(Action ReportProgress, Action Done) { // Checking for cancellation - if (CancellationToken.IsCancellationRequested) return; + if (CancellationToken.IsCancellationRequested) { Done(); return; } for (int i = 0; i <= MaxIterations; i++) { @@ -63,7 +63,7 @@ namespace GrasshopperAsyncComponentDemo.SampleImplementations ReportProgress(Id, ((double)(i + 1) / (double)MaxIterations)); // Checking for cancellation - if (CancellationToken.IsCancellationRequested) return; + if (CancellationToken.IsCancellationRequested) { Done(); return; } } Done(); diff --git a/README.md b/README.md index 1ac1cc4..9ca7383 100644 --- a/README.md +++ b/README.md @@ -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: