Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for intermediate results in IAsync...WithProgress objects #1485

Open
JohnMcPMS opened this issue Jan 30, 2024 · 0 comments
Open

Support for intermediate results in IAsync...WithProgress objects #1485

JohnMcPMS opened this issue Jan 30, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@JohnMcPMS
Copy link
Member

Proposal: Support for intermediate results in IAsync...WithProgress objects

Summary

Currently, the code

if (!IsInRunToCompletionState)
throw CreateCannotGetResultsFromIncompleteOperationException(null);

prevents retrieving any result value before the Task is complete. This feature proposes some ability to set an intermediate result object that could be returned instead of an error.

Rationale

Clients of IAsync...WithProgress are permitted to call GetResults() before the operation has completed, allowing them to observe partial results. This behavior is beneficial for scenarios with non-linear progress. For example, when processing a batch of items, the progress may be for a single item while the intermediate result contains the complete set.

Important Notes

C++/WinRT added this feature with microsoft/cppwinrt#870

A quick read of the code suggests that adding an overload to

public static IAsyncOperationWithProgress<TResult, TProgress> Run<TResult, TProgress>(
Func<CancellationToken, IProgress<TProgress>, Task<TResult>> taskProvider)

to take a result object and return it as the value when GetResults is called would provide the minimum required functionality. A choice could be made to require that the Task result was this same object or not (C++/WinRT does not require that).

An alternative solution would be to also pass an IProgress<TResult> in to the taskProvider, allowing for the intermediate result object to be set inside the task body and also for it to change dynamically, similar to the C++/WinRT implementation.

Open Questions

  1. Is there a more idiomatic way to represent this via existing C# patterns rather than implementing a custom behavior?
    • Task itself offers no support, as it only offers a block-until-completion Result property.
@JohnMcPMS JohnMcPMS added the enhancement New feature or request label Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant