Merge branch 'main' into dim/fixes
This commit is contained in:
@@ -86,6 +86,24 @@ namespace GrasshopperAsyncComponent
|
||||
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)
|
||||
|
||||
+12
-4
@@ -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<string, double> 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);
|
||||
}
|
||||
|
||||
@@ -52,7 +52,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; }
|
||||
|
||||
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();
|
||||
|
||||
@@ -47,7 +47,6 @@ Q: Does this component use all my cores? A: OH YES. It goes WROOOM.
|
||||
|
||||

|
||||
|
||||
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user