-
Notifications
You must be signed in to change notification settings - Fork 60
/
AsyncOperations.cs
116 lines (102 loc) · 4.67 KB
/
AsyncOperations.cs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Functional.Async;
using static System.Drawing.ImageExtensions;
namespace ConsoleApplication1
{
class AsyncOperations
{
void ReadFileBlocking(string filePath, Action<byte[]> process)
{
using (var fileStream = new FileStream(filePath, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
byte[] buffer = new byte[fileStream.Length];
int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
process(buffer);
}
}
// Listing 8.2 Read from the file system asynchronously
IAsyncResult ReadFileNoBlocking(string filePath, Action<byte[]> process)
{
using (var fileStream = new FileStream(filePath, FileMode.Open,
FileAccess.Read, FileShare.Read, 0x1000,
FileOptions.Asynchronous))
{
byte[] buffer = new byte[fileStream.Length];
var state = Tuple.Create(buffer, fileStream, process);
return fileStream.BeginRead(buffer, 0, buffer.Length,
EndReadCallback, state);
}
}
void EndReadCallback(IAsyncResult ar)
{
var state = ar.AsyncState as Tuple<byte[], FileStream, Action<byte[]>>;
using (state.Item2) state.Item2.EndRead(ar);
state.Item3(state.Item1);
}
async void ReadFileNoBlockingAsync(string filePath, Action<byte[]> process)
{
using (var fileStream = new FileStream(filePath, FileMode.Open,
FileAccess.Read, FileShare.Read, 0x1000,
FileOptions.Asynchronous))
{
byte[] buffer = new byte[fileStream.Length];
int bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length);
await Task.Run(async () => process(buffer));
}
}
private Func<string, Task<byte[]>> downloadSiteIcon = async domain =>
{
var response = await new
HttpClient().GetAsync($"http://{domain}/favicon.ico");
return await response.Content.ReadAsByteArrayAsync();
};
// Listing 8.3 Download an image(icon) from the network asynchronously
async Task DownloadIconAsync(string domain, string fileDestination)
{
using (FileStream stream = new FileStream(fileDestination,
FileMode.Create, FileAccess.Write,
FileShare.Write, 0x1000, FileOptions.Asynchronous))
await new HttpClient()
.GetAsync($"http://{domain}/favicon.ico")
.Bind(async content => await
content.Content.ReadAsByteArrayAsync())
.Map(bytes => Image.FromStream(new MemoryStream(bytes))) // # B
.Tap(async image => // #C
await SaveImageAsync(fileDestination, ImageFormat.Jpeg, image));
}
async Task DownloadIconAsyncLINQ(string domain, string fileDestination)
{
using (FileStream stream = new FileStream(fileDestination,
FileMode.Create, FileAccess.Write, FileShare.Write,
0x1000, FileOptions.Asynchronous))
await (from response in new HttpClient()
.GetAsync($"http://{domain}/favicon.ico")
from bytes in response.Content.ReadAsByteArrayAsync()
select stream.WriteAsync(bytes, 0, bytes.Length));
}
void CooperativeCancellation()
{
CancellationTokenSource ctsOne = new CancellationTokenSource(); // #A
CancellationTokenSource ctsTwo = new CancellationTokenSource();// #A
CancellationTokenSource ctsComposite = CancellationTokenSource.CreateLinkedTokenSource(ctsOne.Token, ctsTwo.Token); // #B
CancellationToken ctsCompositeToken = ctsComposite.Token;
Task.Factory.StartNew(async () =>
{
var webClient = new WebClient();
ctsCompositeToken.Register(() => webClient.CancelAsync());
await webClient.DownloadStringTaskAsync("http://www.manning.com");
}, ctsComposite.Token);
}
}
}