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

Copy link
Contributor

@kirsan31 kirsan31 Jun 22, 2025

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.

Yes we are already on UI thread, but in this particular case after WrappedCallbackAsync completed we don't need to do a marshal back on UI thread, so ConfigureAwait(false) is indeed useful here.
But any way I completely agreed with #12697 that it's better to avoid all async staff here and just use BeginInvoke(WrappedCallbackAsync);

BeginInvoke(async () => await WrappedCallbackAsync().ConfigureAwait(false));
return await completion.Task.ConfigureAwait(false);
}
BeginInvoke(async () => await WrappedCallbackAsync());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: BeginInvoke(WrappedCallbackAsync);

@KlausLoeffelmann KlausLoeffelmann force-pushed the FixInvokeAsyncAllocationIssue branch 2 times, most recently from a231d58 to 3d907e2 Compare June 29, 2025 20:18
@KlausLoeffelmann KlausLoeffelmann force-pushed the FixInvokeAsyncAllocationIssue branch from 3d907e2 to bc2c642 Compare July 15, 2025 17:46
@KlausLoeffelmann KlausLoeffelmann force-pushed the FixInvokeAsyncAllocationIssue branch from bc2c642 to 7de829c Compare July 15, 2025 18:14
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.

2 participants