feat(cancellation): exposes cancellation request (example in prime calculator)

This commit is contained in:
Dimitrie Stefanescu
2021-04-03 17:56:39 +01:00
parent b7e57b1ec8
commit 59f87b2a7d
3 changed files with 31 additions and 2 deletions
+20 -1
View File
@@ -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);
}
}
}
@@ -4,7 +4,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{695D2B91-DDB6-416E-8A99-DDE6253DA7AA}</ProjectGuid>
<ProjectGuid>{114D5E49-AC13-47F7-A70E-B4289579F4E3}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GrasshopperAsyncComponent</RootNamespace>
@@ -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