Skip to content

Commit

Permalink
Merge pull request #13 from specklesystems/dim/fixes
Browse files Browse the repository at this point in the history
Dim/fixes
  • Loading branch information
didimitrie authored Apr 3, 2021
2 parents c314fc5 + 8dc8c9a commit a53cef3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
21 changes: 20 additions & 1 deletion GrasshopperAsyncComponent/GH_AsyncComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void RequestCancellation()

public virtual void DisplayProgress(object sender, System.Timers.ElapsedEventArgs e)
{
if (Workers.Count == 0)
if (Workers.Count == 0 || ProgressReports.Values.Count == 0)
{
return;
}
Expand Down Expand Up @@ -244,5 +244,24 @@ protected override void SolveInstance(IGH_DataAccess DA)
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);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown men
this.RequestCancellation();
});
}

public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
{
base.AppendAdditionalMenuItems(menu);
Menu_AppendItem(menu, "Cancel", (s, e) =>
{
RequestCancellation();
});
}
}

public class PrimeCalculatorWorker : WorkerInstance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ protected override void RegisterOutputParams(GH_OutputParamManager pManager)
pManager.AddTextParameter("Output", "O", "Nothing really interesting.", GH_ParamAccess.item);
}

protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu)
public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
{
base.AppendAdditionalComponentMenuItems(menu);
base.AppendAdditionalMenuItems(menu);
Menu_AppendItem(menu, "Cancel", (s, e) =>
{
this.RequestCancellation();
RequestCancellation();
});
}
}
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Looks nice, doesn't it? Notice that the solution runs "eagerly" - every time the

- **Grasshopper and Rhino are still responsive!**
- **There's progress reporting!** (personally I hate waiting for Gh to unfreeze...).
- **Thread safe**: 99% of the times this won't explode in your face. It still might though!

### Approach

Expand Down Expand Up @@ -46,18 +47,20 @@ 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 cancel a stuff that's in progress?
Q: Can I enable cancellation of a longer running task?

A: Yes. The `GH_AsyncComponent` class now exposes the `RequestCancellation()` method, which you can invoke from a custom menu action, or however you want. Note, to properly handle this and ensure the component's inner flow state is properly reset, when respecting the cancellation in your component, you should call the `Done()` function before returning from `DoWork()`. E.g.:
A: Yes, now you can! In your component, just add a right click menu action like so:

```cs

public override void DoWork(Action<string, double> ReportProgress, Action Done)
{
// note: call done from inside DoWork(), then return abruptly.
if (CancellationToken.IsCancellationRequested) { Done(); return; }
// more code
}
public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
{
base.AppendAdditionalMenuItems(menu);
Menu_AppendItem(menu, "Cancel", (s, e) =>
{
RequestCancellation();
});
}

```

Expand Down

0 comments on commit a53cef3

Please sign in to comment.