-
Notifications
You must be signed in to change notification settings - Fork 1k
Refactor/Improve InvokeAsync to address potential disposing issues #13564
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
base: main
Are you sure you want to change the base?
Refactor/Improve InvokeAsync to address potential disposing issues #13564
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the InvokeAsync methods in Control_InvokeAsync.cs to address potential disposing issues and adds improved exception documentation.
- Added a null-check exception comment for the callback parameter.
- Inserted a handle creation check before invoking the asynchronous callback.
- Updated the cancellation token registration handling and its disposal pattern.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
src/System.Windows.Forms/System/Windows/Forms/Control_InvokeAsync.cs | Adds handle creation checks and adjusts cancellation token registration disposal in the async InvokeAsync implementations. |
src/System.Windows.Forms/GlobalSuppressions.cs | Introduces new suppressions for CA2007 warnings on the InvokeAsync methods. |
Comments suppressed due to low confidence (1)
src/System.Windows.Forms/System/Windows/Forms/Control_InvokeAsync.cs:186
- [nitpick] It may be beneficial to add a comment explaining that the cancellation token registration is disposed inside WrappedCallbackAsync to ensure proper unregistration and avoid potential disposal issues in asynchronous contexts.
using (registration)
BeginInvoke(async () => await WrappedCallbackAsync().ConfigureAwait(false)); | ||
await completion.Task.ConfigureAwait(false); | ||
} | ||
BeginInvoke(async () => await WrappedCallbackAsync()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider whether to use ConfigureAwait(false) on the asynchronous lambda passed to BeginInvoke to avoid potentially capturing the synchronization context, unless this behavior is intentionally desired (noting that CA2007 warnings are suppressed).
BeginInvoke(async () => await WrappedCallbackAsync()); | |
BeginInvoke(async () => await WrappedCallbackAsync().ConfigureAwait(false)); |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're already on the UI Thread, and the purpose is to marshal on the UI thread, so, FWIW ConfigureAwait(false)
would not change anything.
Addresses #12696, #12697
(Mentioning the latter just for same topic. I do not think, this is an issue really.)
Microsoft Reviewers: Open in CodeFlow