Skip to content

Commit

Permalink
Fix async task cancellation logic for finding miniscope indices
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnew committed Oct 24, 2024
1 parent 2ddec3d commit a35d13b
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions OpenEphys.Miniscope.Design/UclaMiniscopeSelectionDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Bonsai.Reactive;
using OpenCV.Net;

namespace OpenEphys.Miniscope.Design
Expand All @@ -16,21 +17,22 @@ public partial class UclaMiniscopeSelectionDialog : Form
{
bool scanning = false;
CancellationTokenSource tokenSource = new();
CancellationToken token;
Task cancellableTask;
readonly ScopeKind scopeKind;

public UclaMiniscopeSelectionDialog(ScopeKind kind)
{
InitializeComponent();
token = tokenSource.Token;
scopeKind = kind;
}

void PerformScan(CancellationToken ct, int maxIterations)
{
// Was cancellation already requested?
ct.ThrowIfCancellationRequested();
if (ct.IsCancellationRequested)
{
return;
}

for (int i = 0; i < maxIterations; i++)
{
Expand Down Expand Up @@ -77,7 +79,10 @@ void PerformScan(CancellationToken ct, int maxIterations)
capture.Close();
}

ct.ThrowIfCancellationRequested();
if (ct.IsCancellationRequested)
{
break;
}
}
}

Expand All @@ -88,32 +93,27 @@ void StartScan()
listBox_Indices.Items.Clear();
scanning = true;
}
async void FinishScan()
async void CancelScan()
{
tokenSource.Cancel();
try
{
await cancellableTask;
}
catch (OperationCanceledException)
if (!cancellableTask.IsCanceled)
{
//Console.WriteLine($"\nMain: {nameof(OperationCanceledException)} thrown\n");
tokenSource.Cancel();
}

buttonScan.Text = "Scan";
toolStripStatusLabel.Text = "Idle";
scanning = false;
cancellableTask.Wait();
}

void FinishScanInvoke()
{
buttonScan.Invoke((MethodInvoker)delegate
if (!tokenSource.IsCancellationRequested)
{
buttonScan.Text = "Scan";
});
buttonScan.Invoke((MethodInvoker)delegate
{
buttonScan.Text = "Scan";
});

toolStripStatusLabel.Text = "Idle";
scanning = false;
toolStripStatusLabel.Text = "Idle";
scanning = false;
}
}

private void buttonScan_Click(object sender, EventArgs e)
Expand All @@ -122,26 +122,31 @@ private void buttonScan_Click(object sender, EventArgs e)
{
StartScan();
cancellableTask = Task.Factory.StartNew(() => {
PerformScan(token, 100);
PerformScan(tokenSource.Token, 100);
FinishScanInvoke();
}, token);
}, tokenSource.Token);
}
else
{
FinishScan();
CancelScan();
buttonScan.Text = "Scan";
toolStripStatusLabel.Text = "Idle";
scanning = false;
}
}

private void buttonOK_Click(object sender, EventArgs e)
{
FinishScan();
CancelScan();
tokenSource.Dispose();
DialogResult = DialogResult.OK;
Close();
}

private async void buttonCancel_Click(object sender, EventArgs e)
{
FinishScan();
CancelScan();
tokenSource.Dispose();
DialogResult = DialogResult.Cancel;
Close();
}
Expand Down

0 comments on commit a35d13b

Please sign in to comment.