-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
47 lines (40 loc) · 1.52 KB
/
Program.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
namespace Tpl;
class Program
{
static async Task Main()
{
var w = new HttpClient();
Task<string> webGetTask = w.GetStringAsync("https://endjin.com/");
string pageContent = await webGetTask;
#pragma warning disable CS4014 // The example doesn't do anything with the task returned, but the compiler doesn't like us doing that in an async method. Normally in an async method we just await instead of using ContinueWith
webGetTask.ContinueWith(static t =>
{
string webContent = t.Result;
Console.WriteLine($"Web page length: {webContent.Length}");
});
#pragma warning restore CS4014
}
private static void ShowContinuations()
{
Task op = Task.Run(DoSomething);
var cs = new CancellationTokenSource();
Task onDone = op.ContinueWith(
_ => Console.WriteLine("Never runs"),
cs.Token);
Task andAnotherThing = onDone.ContinueWith(
_ => Console.WriteLine("Continuation's continuation"));
cs.Cancel();
}
static void DoSomething()
{
Thread.Sleep(1000);
Console.WriteLine("Initial task finishing");
}
}
// This example is illustrative and is not meant to compile, hence the #if false.
#if false
public virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int count,
AsyncCallback callback, object state) ...
public virtual void EndWrite(IAsyncResult asyncResult) ...
public abstract void Write(byte[] buffer, int offset, int count) ...
#endif