Merge branch 'main' into dim/fixes

This commit is contained in:
Dimitrie Stefanescu
2021-04-03 18:07:21 +01:00
committed by GitHub
4 changed files with 32 additions and 7 deletions
@@ -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)
@@ -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();
-1
View File
@@ -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: