forked from tunnelvisionlabs/dotnet-threading
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskBlockWhileAsync.cs
More file actions
66 lines (59 loc) · 1.98 KB
/
Copy pathTaskBlockWhileAsync.cs
File metadata and controls
66 lines (59 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) Rackspace, US Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
namespace CSharpSamples
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rackspace.Threading;
/// <summary>
/// This class contains unit-tested example code for the <see cref="TaskBlocks.While(Func{bool}, Func{Task})"/>
/// building block method.
/// </summary>
[TestClass]
public class TaskBlockWhileAsync
{
[TestMethod]
public async Task TestWhileAsyncAwait()
{
await WhileAsyncAwait();
}
[TestMethod]
public async Task TestWhile()
{
await While();
}
#pragma warning disable 1998 // This async method lacks 'await' operators and will run synchronously....
#region WhileAsyncAwait
public async Task WhileAsyncAwait()
{
Stack<int> workList = new Stack<int>(new[] { 3, 5, 7 });
while (workList.Count > 0)
{
await ProcessWorkItemAsyncAwait(workList.Pop());
}
}
private async Task ProcessWorkItemAsyncAwait(int item)
{
// this would typically perform an asynchronous operation on the
// work item
}
#endregion
#pragma warning restore 1998
#region WhileAsyncBuildingBlock
public Task While()
{
Stack<int> workList = new Stack<int>(new[] { 3, 5, 7 });
return TaskBlocks.While(
() => workList.Count > 0,
() => ProcessWorkItemAsync(workList.Pop()));
}
private Task ProcessWorkItemAsync(int item)
{
// this would typically perform an asynchronous operation on the
// work item
return CompletedTask.Default;
}
#endregion
}
}