-
Notifications
You must be signed in to change notification settings - Fork 6k
Fix cancellation by polling example (#31643) #31695
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
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.
Thanks for updating this @JJS
I've reviewed these changes, and I'll now.
Is there something to do about the failed snippet? The error seems not to be related to the issue |
Yes, if you create a default .csproj and .vbproj in the two folders with these source files, that will fix the issue. It's a historical artifact. The source code was imported into GitHub from an internal system that built the samples when edited. There weren't project files, and we've been adding them whenever we edit any of the existing samples. |
} | ||
if (token.IsCancellationRequested) { | ||
// Cleanup or undo here if necessary... | ||
Console.WriteLine("\r\nCancelling after row {0}.", x); |
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.
Is x
in scope here?
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.
I don't see how cancellation is useful after the heavy work is already done?
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.
true, x
is not in scope, I'll fix that.
As the usefulness of the cancellation is concerned, the overall behavior of the code did not change.
The polling example's main part is the IsCancellationRequested
check in the loop condition, which did not change.
I suppose you could remove the additional if
block, which is only there to provide a place for clean up code in case of cancellation. But you might need a place to put the ThrowIfCancellationRequested
call.
I am not sure what you mean by "heavy work is already done" as the outer loop can be left at any point when cancellation is requested.
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.
I could be misunderstanding something (and in fact I'm not sure what's the issue being resolved here), but I see token.IsCancellationRequested
is done after the loops have already finished and completed the work.
This also doesn't make sense for me with the existing comment that says:
// Assume that we know that the inner loop is very fast. // Therefore, checking once per row in the outer loop is sufficient.
What do you think @BillWagner?
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.
I updated the scope, the comments and some other issues with mixed up columns/rows.
token.IsCancellationRequested
is called in the loop condition (so polling each column), not only after the loops. So the loops may not have completed their work. As I understand the original example, the call to token.IsCancellationRequested
after the loops is only for clean up or calling token.ThrowIfCancellationRequested
.
As the linked issue correctly states, there is an unexpected behavior if the cancellation is requested after the if
condition is evaluated and before the loop condition is evaluated. In this case, the clean up code is not called.
As I said, there is a point to removing the if
block completely but there must be some place to put the token.ThrowIfCancellationRequested
in.
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.
token.IsCancellationRequested is called in the loop condition
Ah I missed that.
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.
A few nits
samples/snippets/csharp/VS_Snippets_Misc/cancellation/cs/cancellation.csproj
Outdated
Show resolved
Hide resolved
samples/snippets/csharp/VS_Snippets_Misc/cancellation/cs/cancellationex11.cs
Outdated
Show resolved
Hide resolved
samples/snippets/visualbasic/VS_Snippets_Misc/cancellation/vb/cancellation.vbproj
Outdated
Show resolved
Hide resolved
samples/snippets/csharp/VS_Snippets_Misc/cancellation/cs/cancellationex11.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: David Pine <david.pine@microsoft.com>
Summary
I moved the
token.IsCancellationRequested
-block out of the outer loop to ensure it is checked in all cases.Fixes #31643