Skip to content

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

KlausLoeffelmann
Copy link
Member

@KlausLoeffelmann KlausLoeffelmann commented Jun 7, 2025

Addresses #12696, #12697
(Mentioning the latter just for same topic. I do not think, this is an issue really.)

Microsoft Reviewers: Open in CodeFlow

Copy link
Contributor

@Copilot Copilot AI left a 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());
Copy link
Preview

Copilot AI Jun 7, 2025

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

Suggested change
BeginInvoke(async () => await WrappedCallbackAsync());
BeginInvoke(async () => await WrappedCallbackAsync().ConfigureAwait(false));

Copilot uses AI. Check for mistakes.

Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant