C#
C#

help

Root Question Message

mrphil2105
mrphil210511/7/2022
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);
}

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...
}

What are your thoughts?
Tvde1
Tvde111/7/2022
it's a little weird. You don't want logging or UI updates to hamper your downloading
Tvde1
Tvde111/7/2022
I'd probably do this with a Rx subject. You can probably configure those to invoke observers on a different Task thread
mrphil2105
mrphil210511/7/2022
I don't use Rx
mrphil2105
mrphil210511/7/2022
I might just use a regular event :when:
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy