Skip to content

Commit

Permalink
Clearer recording of cancellation events
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed Aug 11, 2020
1 parent adf6869 commit e581c9a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Drums/VDrumExplorer.Gui/Dialogs/DataTransferDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Style="{StaticResource WindowStyle}"
Closing="HandleClosing">
<StackPanel Margin="6">
<TextBlock Text="{Binding CurrentItem}" />
<TextBlock Text="{Binding ProgressDescription}" />
<Grid Margin="0,8,0,0" Height="23">
<ProgressBar Value="{Binding Completed, Mode=OneWay}" Maximum="{Binding Total}" />
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
Expand Down
19 changes: 13 additions & 6 deletions Drums/VDrumExplorer.Model/Device/DeviceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ private async Task<ModuleDataSnapshot> LoadDescendantsAsync(TreeNode root, IProg
int completed = 0;
foreach (var container in containers)
{
progressHandler?.Report(new TransferProgress(completed, containers.Count, $"Copying {container.Path}"));
progressHandler?.Report(new TransferProgress(completed, containers.Count, container.Path));
var segment = await LoadSegment(container.Address, container.Size, cancellationToken);
completed++;
snapshot.Add(segment);
}
progressHandler?.Report(new TransferProgress(containers.Count, containers.Count, "Complete"));
progressHandler?.Report(new TransferProgress(containers.Count, containers.Count, "complete"));
return snapshot;
}

Expand All @@ -143,8 +143,15 @@ private async Task<DataSegment> LoadSegment(ModuleAddress address, int size, Can
{
var timerToken = new CancellationTokenSource(loadSegmentTimeout).Token;
var effectiveToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timerToken).Token;
var data = await client.RequestDataAsync(address.DisplayValue, size, effectiveToken);
return new DataSegment(address, data);
try
{
var data = await client.RequestDataAsync(address.DisplayValue, size, effectiveToken);
return new DataSegment(address, data);
}
catch (OperationCanceledException) when (timerToken.IsCancellationRequested)
{
throw new TimeoutException("Timed out waiting for data from device");
}
}

public async Task<string> LoadKitNameAsync(int kit, CancellationToken cancellationToken)
Expand Down Expand Up @@ -175,11 +182,11 @@ private async Task SaveSnapshot(ModuleDataSnapshot snapshot, Dictionary<ModuleAd
int completed = 0;
foreach (var segment in snapshot.Segments)
{
progressHandler?.Report(new TransferProgress(completed, snapshot.SegmentCount, $"Copying {addressPaths[segment.Address]}"));
progressHandler?.Report(new TransferProgress(completed, snapshot.SegmentCount, addressPaths[segment.Address]));
await SaveSegment(segment, cancellationToken);
completed++;
}
progressHandler?.Report(new TransferProgress(snapshot.SegmentCount, snapshot.SegmentCount, "Complete"));
progressHandler?.Report(new TransferProgress(snapshot.SegmentCount, snapshot.SegmentCount, "complete"));
}

private async Task SaveSegment(DataSegment segment, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected async void CopyDataToDevice(DataTreeNode? node, ModuleAddress? targetA
return;
}
// We may or may not really need to bring up the dialog box, but it's simplest to always do that.
var viewModel = new DataTransferViewModel<string>(Logger, "Copying data to device",
var viewModel = new DataTransferViewModel<string>(Logger, "Copying data to device", "Copying {0}",
async (progress, token) => { await device.SaveDescendants(node, targetAddress, progress, token); return ""; });
await ViewServices.ShowDataTransferDialog(viewModel);
}
Expand Down
19 changes: 14 additions & 5 deletions Drums/VDrumExplorer.ViewModel/Dialogs/DataTransferViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ public int Total
protected set => SetProperty(ref total, value);
}

private string currentItem = "Progress";
private string currentItem = "";
public string CurrentItem
{
get => currentItem;
protected set => SetProperty(ref currentItem, value);
}

private string progressDescription = "Progress";
public string ProgressDescription
{
get => progressDescription;
protected set => SetProperty(ref progressDescription, value);
}

private bool? dialogResult;
public bool? DialogResult
{
Expand All @@ -61,14 +68,16 @@ public DataTransferViewModel(string title)

public sealed class DataTransferViewModel<T> : DataTransferViewModel
{
private readonly string progressFormat;
private readonly ILogger logger;
private readonly Func<IProgress<TransferProgress>, CancellationToken, Task<T>> transferFunction;

public DataTransferViewModel(ILogger logger, string title, Func<IProgress<TransferProgress>, CancellationToken, Task<T>> transferFunction)
public DataTransferViewModel(ILogger logger, string title, string progressFormat, Func<IProgress<TransferProgress>, CancellationToken, Task<T>> transferFunction)
: base(title)
{
this.transferFunction = transferFunction;
this.logger = logger;
this.progressFormat = progressFormat;
this.transferFunction = transferFunction;
}

public async Task<T> TransferAsync()
Expand All @@ -87,7 +96,7 @@ public async Task<T> TransferAsync()
}
catch (Exception e)
{
logger.LogError(e, $"Failure while loading {CurrentItem}");
logger.LogError(e, $"Failure while transferring {CurrentItem}");
throw;
}
finally
Expand All @@ -97,6 +106,6 @@ public async Task<T> TransferAsync()
}

private void UpdateProgress(TransferProgress progress) =>
(Total, Completed, CurrentItem) = (progress.Total, progress.Completed, progress.Current);
(Total, Completed, ProgressDescription, CurrentItem) = (progress.Total, progress.Completed, string.Format(progressFormat, progress.Current), progress.Current);
}
}
4 changes: 2 additions & 2 deletions Drums/VDrumExplorer.ViewModel/Home/ExplorerHomeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private async void LoadModuleFromDevice()
{
return;
}
var transferViewModel = new DataTransferViewModel<Module>(logger, "Loading module data", device.LoadModuleAsync);
var transferViewModel = new DataTransferViewModel<Module>(logger, "Loading module data", "Loading {0}", device.LoadModuleAsync);
var module = await viewServices.ShowDataTransferDialog(transferViewModel);
if (module is object)
{
Expand All @@ -93,7 +93,7 @@ private async void LoadKitFromDevice()
return;
}
var kitNumber = LoadKitFromDeviceNumber;
var transferViewModel = new DataTransferViewModel<Kit>(logger, "Loading kit data", (progress, token) => device.LoadKitAsync(kitNumber, progress, token));
var transferViewModel = new DataTransferViewModel<Kit>(logger, "Loading kit data", "Loading {0}", (progress, token) => device.LoadKitAsync(kitNumber, progress, token));
var kit = await viewServices.ShowDataTransferDialog(transferViewModel);
if (kit is object)
{
Expand Down

0 comments on commit e581c9a

Please sign in to comment.