Skip to content

Commit 5e6a9fd

Browse files
oldnewthingSteven White
authored andcommitted
Merged PR 21067: Clarify IAsyncInfo.Closed and IAsyncXxx.Completed
Clarify IAsyncInfo.Closed and IAsyncXxx.Completed Closed: * May not call before operation has completed. * Implementation permitted to release any associated resources. * May not call any methods or access any properties. Completed: * If you set it after the operation has completed, then the handler is called back "immediately" (subject to whatever the operation's policy is), possibly even recursively from within the setter. This resolves customer issues * https://stackoverflow.com/questions/78007860/iasyncoperation-completed-handler-race-conditions * #2455
1 parent b5340f8 commit 5e6a9fd

5 files changed

Lines changed: 50 additions & 25 deletions

windows.foundation/iasyncaction_completed.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@ public Windows.Foundation.AsyncActionCompletedHandler Completed { get; set; }
1010
# Windows.Foundation.IAsyncAction.Completed
1111

1212
## -description
13-
Gets or sets the method that handles the action completed notification.
13+
Gets or sets the delegate that is called when the action completes.
1414

1515
## -property-value
16-
The method that handles the notification.
16+
The delegate that is called when the action completes.
1717

1818
## -remarks
19-
The Windows Runtime enforces that this property can only be set once on an action.
19+
You're not allowed to set the **Completed** property more than once.
2020

21-
Generally, a completed [IAsyncAction](iasyncaction.md) method called using language-specific awaitable syntax does nothing further than to return **null** when it completes.
21+
Most applications don't use the **Completed** property directly,
22+
but instead use a language-specific syntax for awaiting the completion
23+
of an asynchronous action,
24+
such as `co_await` (C++/WinRT), `await` (C#, Javascript), or `then` (Javascript, C++/CX).
2225

23-
If you're implementing [IAsyncAction](iasyncaction.md), then the set implementation of **Completed** should store the handler, and the surrounding logic should invoke it when [Close](iasyncinfo_close_811482585.md) is called. The implementation should set the *asyncStatus* parameter of invoked callbacks appropriately if there is a [Cancel](iasyncinfo_cancel_1985564044.md) call, [Status](iasyncinfo_status.md) is not **Completed**, errors occurred, and so on.
26+
If the **Completed** property is set after the action has already completed,
27+
then the action behaves as if it had completed immediately after the handler was received.
28+
Note that this can result in the handler being called before the **Completed** property setter
29+
has returned; possibly even from the same thread.
2430

2531
<!--Needed- topic on roll-your-own async that covers stuff like that-->
2632

windows.foundation/iasyncactionwithprogress_1_completed.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,28 @@ public Windows.Foundation.AsyncActionWithProgressCompletedHandler<TProgress> Com
1010
# Windows.Foundation.IAsyncActionWithProgress<TProgress>.Completed
1111

1212
## -description
13-
Gets or sets the method that handles the action completed notification.
13+
Gets or sets the delegate that is called when the action completes.
1414

1515
## -property-value
16-
The method that handles the notification.
16+
The delegate that is called when the action completes.
1717

1818
## -remarks
19-
The Windows Runtime enforces that this property can only be set once on an action.
19+
You're not allowed to set the **Completed** property more than once.
2020

21-
Generally, a completed [IAsyncActionWithProgress<TProgress>](iasyncactionwithprogress_1.md) method called using language-specific awaitable syntax does nothing further than to return **null** when it completes.
22-
23-
If you're implementing [IAsyncActionWithProgress<TProgress>](iasyncactionwithprogress_1.md), then the set implementation of [Completed](iasyncoperationwithprogress_2_completed.md) should store the handler, and the surrounding logic should invoke it when [Close](iasyncinfo_close_811482585.md) is called. The implementation should set the *asyncStatus* parameter of invoked callbacks appropriately if there is a [Cancel](iasyncinfo_cancel_1985564044.md) call, [Status](iasyncinfo_status.md) is not **Completed**, errors occurred, and so on.
21+
Most applications don't use the **Completed** property directly,
22+
but instead use a language-specific syntax for awaiting the completion
23+
of an asynchronous action,
24+
such as `co_await` (C++/WinRT), `await` (C#, Javascript), or `then` (Javascript, C++/CX).
2425

26+
If the **Completed** property is set after the action has already completed,
27+
then the action behaves as if it had completed immediately after the handler was received.
28+
Note that this can result in the handler being called before the **Completed** property setter
29+
has returned; possibly even from the same thread.
2530

2631
<!--Needed- topic on roll-your-own async that covers stuff like that-->
2732

2833
## -examples
2934
For example [C++/WinRT](/windows/uwp/cpp-and-winrt-apis/) code illustrating how to handle the **Completed** event, see [Delegate types for asynchronous actions and operations](/windows/uwp/cpp-and-winrt-apis/handle-events#delegate-types-for-asynchronous-actions-and-operations).
3035

3136
## -see-also
32-
[IAsyncActionWithProgress&lt;TProgress&gt;](iasyncactionwithprogress_1.md)
37+
[IAsyncActionWithProgress&lt;TProgress&gt;](iasyncactionwithprogress_1.md)

windows.foundation/iasyncinfo_close_811482585.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ public void Close()
1313
Closes the asynchronous operation.
1414

1515
## -remarks
16-
Calling this method indicates that you have finished with the results of the operation. After calling Close, do not call the **GetResults** method again (each of the 4 [IAsyncInfo](iasyncinfo.md) derived interfaces have their own implementation of **GetResults**.)
16+
Calling this method indicates that you have finished with the asynchronous action or operation,
17+
and it is permitted to free any associated resources.
18+
After calling **Close**, you may not call any methods or access any properties
19+
of the object that implements the [IAsyncInfo](iasyncinfo.md) interface.
1720

18-
<!--Is this a Notes to Callers? It's a weird note for implementation purposes.-->
21+
You may not call **Close** before the asynchronous action or operation has completed.
1922

2023
## -examples
2124

windows.foundation/iasyncoperation_1_completed.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ public Windows.Foundation.AsyncOperationCompletedHandler<TResult> Completed { ge
1010
# Windows.Foundation.IAsyncOperation<TResult>.Completed
1111

1212
## -description
13-
Gets or sets the method that handles the operation completed notification.
13+
Gets or sets the delegate that is called when the operation completes.
1414

1515
## -property-value
16-
The method that handles the notification.
16+
The delegate that is called when the operation completes.
1717

1818
## -remarks
19-
The Windows Runtime enforces that this property can only be set once on an operation.
19+
You're not allowed to set the **Completed** property more than once.
2020

21-
Generally, a completed [IAsyncOperation<TResult>](iasyncoperation_1.md) method called using awaitable syntax does nothing further than to return its result (an object of the **TResult** type) when it completes.
22-
23-
If you're implementing [IAsyncOperation<TResult>](iasyncoperation_1.md), then the set implementation of [Completed](iasyncaction_completed.md) should store the handler, and the surrounding logic should invoke it when [Close](iasyncinfo_close_811482585.md) is called. The implementation should set the *asyncStatus* parameter of invoked callbacks appropriately if there is a [Cancel](iasyncinfo_cancel_1985564044.md) call, [Status](iasyncinfo_status.md) is not **Completed**, errors occurred, and so on.
21+
Most applications don't use the **Completed** property directly,
22+
but instead use a language-specific syntax for awaiting the completion
23+
of an asynchronous action,
24+
such as `co_await` (C++/WinRT), `await` (C#, Javascript), or `then` (Javascript, C++/CX).
2425

26+
If the **Completed** property is set after the action has already completed,
27+
then the action behaves as if it had completed immediately after the handler was received.
28+
Note that this can result in the handler being called before the **Completed** property setter
29+
has returned; possibly even from the same thread.
2530

2631
<!--Needed- topic on roll-your-own async that covers stuff like that-->
2732

windows.foundation/iasyncoperationwithprogress_2_completed.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@ public Windows.Foundation.AsyncOperationWithProgressCompletedHandler<TResult, TP
1010
# Windows.Foundation.IAsyncOperationWithProgress<TResult, TProgress>.Completed
1111

1212
## -description
13-
Gets or sets the method that handles the operation completed notification.
13+
Gets or sets the delegate that is called when the operation completes.
1414

1515
## -property-value
16-
The method that handles the notification.
16+
The delegate that is called when the operation completes.
1717

1818
## -remarks
19-
The Windows Runtime enforces that this property can only be set once on an operation.
19+
You're not allowed to set the **Completed** property more than once.
2020

21-
Generally, a completed [AsyncOperationWithProgressCompletedHandler<TResult, TProgress>](asyncoperationwithprogresscompletedhandler_2.md) method called using awaitable syntax does nothing further than to return its result (an object of the **TResult** type) when it completes.
21+
Most applications don't use the **Completed** property directly,
22+
but instead use a language-specific syntax for awaiting the completion
23+
of an asynchronous action,
24+
such as `co_await` (C++/WinRT), `await` (C#, Javascript), or `then` (Javascript, C++/CX).
2225

23-
If you're implementing [AsyncOperationWithProgressCompletedHandler<TResult, TProgress>](asyncoperationwithprogresscompletedhandler_2.md), then the set implementation of [Completed](iasyncaction_completed.md) should store the handler, and the surrounding logic should invoke it when [Close](iasyncinfo_close_811482585.md) is called. The implementation should set the *asyncStatus* parameter of invoked callbacks appropriately if there is a [Cancel](iasyncinfo_cancel_1985564044.md) call, [Status](iasyncinfo_status.md) is not **Completed**, errors occurred, and so on.
26+
If the **Completed** property is set after the action has already completed,
27+
then the action behaves as if it had completed immediately after the handler was received.
28+
Note that this can result in the handler being called before the **Completed** property setter
29+
has returned; possibly even from the same thread.
2430

2531
## -examples
2632
For example [C++/WinRT](/windows/uwp/cpp-and-winrt-apis/) code illustrating how to handle the **Completed** event, see [Delegate types for asynchronous actions and operations](/windows/uwp/cpp-and-winrt-apis/handle-events#delegate-types-for-asynchronous-actions-and-operations).

0 commit comments

Comments
 (0)