feat(cancellation): exposes task cancellation method

This commit is contained in:
Dimitrie Stefanescu
2021-04-03 17:10:36 +01:00
parent b7e57b1ec8
commit c314fc5834
5 changed files with 61 additions and 7 deletions
@@ -6,6 +6,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GrasshopperAsyncComponent;
using System.Windows.Forms;
namespace GrasshopperAsyncComponentDemo.SampleImplementations
{
@@ -30,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();
});
}
}
@@ -44,7 +53,7 @@ namespace GrasshopperAsyncComponentDemo.SampleImplementations
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;
@@ -53,14 +62,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)
{
@@ -98,7 +107,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);
}