Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/Controls/src/Core/ImageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ public virtual Task<bool> Cancel()
if (!IsLoading)
return Task.FromResult(false);

var tcs = new TaskCompletionSource<bool>();
TaskCompletionSource<bool> original = Interlocked.CompareExchange(ref _completionSource, tcs, null);
if (original == null)
TaskCompletionSource<bool> original = Interlocked.CompareExchange(ref _completionSource, new TaskCompletionSource<bool>(), null);
if (original is null)
{
_cancellationTokenSource.Cancel();
return Task.FromResult(false);
}
else
tcs = original;

return tcs.Task;
return original.Task;
}

/// <include file="../../docs/Microsoft.Maui.Controls/ImageSource.xml" path="//Member[@MemberName='FromFile']/Docs/*" />
Expand Down
11 changes: 11 additions & 0 deletions src/Controls/tests/Core.UnitTests/ImageSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,16 @@ public void ImplicitCastOnAbsolutePathsShouldCreateAFileImageSource()
var image = new Image { Source = path };
Assert.IsType<FileImageSource>(image.Source);
}

[Fact]
public async Task CancelCompletes()
{
var imageSource = new StreamImageSource
{
Stream = _ => Task.FromResult<Stream>(new MemoryStream())
};
await ((IStreamImageSource)imageSource).GetStreamAsync();
await imageSource.Cancel(); // This should complete!
}
}
}