-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathasync.cs
More file actions
77 lines (66 loc) · 2.19 KB
/
async.cs
File metadata and controls
77 lines (66 loc) · 2.19 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
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.IO;
using System.Threading.Tasks;
namespace Semmle
{
public class Asynchronous
{
private static string[] filenames = { "async.cs", "Await.ql" };
private static void Main(string[] args)
{
Task[] results = new Task[filenames.Length];
for (int i = 0; i < results.Length; i++)
results[i] = PrintContentLengthAsync(filenames[i]);
for (int i = 0; i < results.Length; i++)
results[i].Wait();
}
private static async Task PrintContentLengthAsync(string filename)
{
int length = await ContentLengthAsync(filename);
Console.WriteLine("Fetched " + length + " characters");
}
private static async Task<int> ContentLengthAsync(string filename)
{
using (StreamReader reader = File.OpenText(filename))
{
string content = await reader.ReadToEndAsync();
return content.Length;
}
}
// regression test for 'AddResumePoint' error
private static async void AsyncIterator(string filename)
{
using (StreamReader other = File.OpenText(filename))
{
using (StreamReader reader = await OpenAsync(filename))
{
string content = await reader.ReadToEndAsync();
Console.WriteLine(content.Length);
}
}
}
private static async Task<StreamReader> OpenAsync(string filename)
{
await PrintContentLengthAsync(filename);
return File.OpenText(filename);
}
private ref struct RS
{
public int GetZero() { return 0; }
}
private static int one = 1;
// Test that we can use ref locals, ref structs and unsafe blocks in async methods.
private static async Task<int> GetObjectAsync()
{
unsafe
{
// Do pointer stuff
}
RS rs;
ref int i = ref one;
var zero = rs.GetZero();
await Task.Delay(i);
return zero;
}
}
}