C
C#3y ago
mrphil2105

Reporting download progress with IAsyncEnumerable

Is reporting progress with an IAsyncEnumerable<T> weird? I have the following:
public interface IDownloadService
{
IAsyncEnumerable<DownloadProgress> DownloadAsync(CancellationToken cancellationToken = default);
}
public interface IDownloadService
{
IAsyncEnumerable<DownloadProgress> DownloadAsync(CancellationToken cancellationToken = default);
}
Is this a weird thing to do? I know it will "pause" the download in-between each progress yield (that is yield return new DownloadProgress(...)). But I kind of like the way you consume it:
var enumerable = _downloadService.DownloadAsync(cancellationToken);

await foreach (var progress in enumerable.WithCancellation(cancellationToken))
{
// Use 'progress' here...
}
var enumerable = _downloadService.DownloadAsync(cancellationToken);

await foreach (var progress in enumerable.WithCancellation(cancellationToken))
{
// Use 'progress' here...
}
What are your thoughts?
2 Replies
Tvde1
Tvde13y ago
it's a little weird. You don't want logging or UI updates to hamper your downloading I'd probably do this with a Rx subject. You can probably configure those to invoke observers on a different Task thread
mrphil2105
mrphil2105OP3y ago
I don't use Rx I might just use a regular event when
Want results from more Discord servers?
Add your server