Skip to content

Commit 5d91ed5

Browse files
JJSBillWagnerIEvangelist
authored
Fix cancellation by polling example (#31643) (#31695)
* Fix cs example by moving check out of loop (#31643) * Fix vs example by moving check out of loop (#31643) * Fix variable scope and comments (#31643) * Add project files (#31643) * Apply suggestions from code review Co-authored-by: David Pine <david.pine@microsoft.com> Co-authored-by: Bill Wagner <wiwagn@microsoft.com> Co-authored-by: David Pine <david.pine@microsoft.com>
1 parent 532a46c commit 5d91ed5

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

samples/snippets/csharp/VS_Snippets_Misc/cancellation/cs/cancellationex11.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,24 @@ static void Main()
4040
//<snippet3>
4141
static void NestedLoops(Rectangle rect, CancellationToken token)
4242
{
43-
for (int x = 0; x < rect.columns && !token.IsCancellationRequested; x++) {
44-
for (int y = 0; y < rect.rows; y++) {
43+
for (int col = 0; col < rect.columns && !token.IsCancellationRequested; col++) {
44+
// Assume that we know that the inner loop is very fast.
45+
// Therefore, polling once per column in the outer loop condition
46+
// is sufficient.
47+
for (int row = 0; row < rect.rows; row++) {
4548
// Simulating work.
46-
Thread.SpinWait(5000);
47-
Console.Write("{0},{1} ", x, y);
49+
Thread.SpinWait(5_000);
50+
Console.Write("{0},{1} ", col, row);
4851
}
52+
}
4953

50-
// Assume that we know that the inner loop is very fast.
51-
// Therefore, checking once per row is sufficient.
52-
if (token.IsCancellationRequested) {
53-
// Cleanup or undo here if necessary...
54-
Console.WriteLine("\r\nCancelling after row {0}.", x);
55-
Console.WriteLine("Press any key to exit.");
56-
// then...
57-
break;
58-
// ...or, if using Task:
59-
// token.ThrowIfCancellationRequested();
60-
}
54+
if (token.IsCancellationRequested) {
55+
// Cleanup or undo here if necessary...
56+
Console.WriteLine("\r\nCancelling before column {0}.", col);
57+
Console.WriteLine("Press any key to exit.");
58+
59+
// If using Task:
60+
// token.ThrowIfCancellationRequested();
6161
}
6262
}
6363
//</snippet3>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

samples/snippets/visualbasic/VS_Snippets_Misc/cancellation/vb/cancellationex11.vb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,26 @@ Class CancelByPolling
4141

4242
'<snippet3>
4343
Shared Sub NestedLoops(ByVal rect As Rectangle, ByVal token As CancellationToken)
44-
For x As Integer = 0 To rect.columns
45-
For y As Integer = 0 To rect.rows
44+
Dim col As Integer
45+
For col = 0 To rect.columns - 1
46+
' Assume that we know that the inner loop is very fast.
47+
' Therefore, polling once per column in the outer loop condition
48+
' is sufficient.
49+
For col As Integer = 0 To rect.rows - 1
4650
' Simulating work.
4751
Thread.SpinWait(5000)
48-
Console.Write("0' end block,1' end block ", x, y)
52+
Console.Write("0',1' ", x, y)
4953
Next
50-
51-
' Assume that we know that the inner loop is very fast.
52-
' Therefore, checking once per row is sufficient.
53-
If token.IsCancellationRequested = True Then
54-
' Cleanup or undo here if necessary...
55-
Console.WriteLine(vbCrLf + "Cancelling after row 0' end block.", x)
56-
Console.WriteLine("Press any key to exit.")
57-
' then...
58-
Exit For
59-
' ...or, if using Task:
60-
' token.ThrowIfCancellationRequested()
61-
End If
6254
Next
55+
56+
If token.IsCancellationRequested = True Then
57+
' Cleanup or undo here if necessary...
58+
Console.WriteLine(vbCrLf + "Cancelling before column 0'.", col)
59+
Console.WriteLine("Press any key to exit.")
60+
61+
' If using Task:
62+
' token.ThrowIfCancellationRequested()
63+
End If
6364
End Sub
6465
'</snippet3>
6566
End Class

0 commit comments

Comments
 (0)