diff --git a/GrasshopperAsyncComponent/GH_AsyncComponent.cs b/GrasshopperAsyncComponent/GH_AsyncComponent.cs index 33179f9..3178cd3 100755 --- a/GrasshopperAsyncComponent/GH_AsyncComponent.cs +++ b/GrasshopperAsyncComponent/GH_AsyncComponent.cs @@ -88,7 +88,7 @@ namespace GrasshopperAsyncComponent public virtual void DisplayProgress(object sender, System.Timers.ElapsedEventArgs e) { - if (Workers.Count == 0) + if (Workers.Count == 0 || ProgressReports.Values.Count == 0) { return; } @@ -226,5 +226,24 @@ namespace GrasshopperAsyncComponent Message = "Done"; OnDisplayExpired(true); } + + public void RequestCancellation() + { + foreach (var source in CancellationSources) + { + source.Cancel(); + } + + CancellationSources.Clear(); + Workers.Clear(); + ProgressReports.Clear(); + Tasks.Clear(); + + Interlocked.Exchange(ref State, 0); + Interlocked.Exchange(ref SetData, 0); + Message = "Cancelled"; + OnDisplayExpired(true); + } + } } diff --git a/GrasshopperAsyncComponent/GrasshopperAsyncComponent.csproj b/GrasshopperAsyncComponent/GrasshopperAsyncComponent.csproj index 8a8db8d..c48c592 100755 --- a/GrasshopperAsyncComponent/GrasshopperAsyncComponent.csproj +++ b/GrasshopperAsyncComponent/GrasshopperAsyncComponent.csproj @@ -4,7 +4,7 @@ Debug AnyCPU - {695D2B91-DDB6-416E-8A99-DDE6253DA7AA} + {114D5E49-AC13-47F7-A70E-B4289579F4E3} Library Properties GrasshopperAsyncComponent diff --git a/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs b/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs index 0cd09e1..22b4e0b 100755 --- a/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs +++ b/GrasshopperAsyncComponentDemo/SampleImplementations/Sample_PrimeCalculatorAsyncComponent.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using GrasshopperAsyncComponent; +using System.Windows.Forms; namespace GrasshopperAsyncComponentDemo.SampleImplementations { @@ -32,6 +33,15 @@ namespace GrasshopperAsyncComponentDemo.SampleImplementations pManager.AddNumberParameter("Output", "O", "The n-th prime number.", GH_ParamAccess.item); } + + public override void AppendAdditionalMenuItems(ToolStripDropDown menu) + { + base.AppendAdditionalMenuItems(menu); + Menu_AppendItem(menu, "Cancel", (s, e) => + { + RequestCancellation(); + }); + } } public class PrimeCalculatorWorker : WorkerInstance