Skip to content

Instantly share code, notes, and snippets.

@SelStrom
Last active August 15, 2021 21:01
Show Gist options
  • Save SelStrom/0e0f414c9ddc5b13303ca64867f394d0 to your computer and use it in GitHub Desktop.
Save SelStrom/0e0f414c9ddc5b13303ca64867f394d0 to your computer and use it in GitHub Desktop.
parallel process on awake
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AwaitExperiments
{
class Program
{
private static CancellationTokenSource _tokenSource;
private static int _i;
static async Task Main(string[] args)
{
_tokenSource = new CancellationTokenSource();
var token = _tokenSource.Token;
try
{
await MainTaskAsync(token);
}
catch (OperationCanceledException _)
{
Console.WriteLine($"Was canceled i: {_i}");
}
finally
{
Console.WriteLine($"Finally call i: {_i}");
}
/*Console.WriteLine("[Another job] Start");
await Task.Delay(3000);
Console.WriteLine("[Another job] Complete");
Console.WriteLine($"Complete normaly i: {_i}");*/
}
private static async Task MainTaskAsync(CancellationToken ct)
{
_i = 0;
var someWorkOneTask = SomeWorkOneAsync(ct);
++_i;
await Task.Delay(1200, ct);
++_i;
var someWorkTwoTask = SomeWorkTwoAsync(someWorkOneTask, ct);
/*++_i;
await someWorkTwoTask;*/
++_i;
await someWorkTwoTask;
ct.ThrowIfCancellationRequested();
++_i;
ct.ThrowIfCancellationRequested();
++_i;
ct.ThrowIfCancellationRequested();
++_i;
if (ct.IsCancellationRequested)
{
Console.WriteLine($"[MainTaskAsync] cancelation reuqested i: {_i}");
ct.ThrowIfCancellationRequested();
}
await LastWorkAsync(ct);
++_i;
}
private static async Task LastWorkAsync(CancellationToken ct)
{
Console.WriteLine($"[LastWorkAsync] start i: {_i}");
if (ct.IsCancellationRequested)
{
Console.WriteLine($"[LastWorkAsync] cancelation reuqested i: {_i}");
ct.ThrowIfCancellationRequested();
}
await Task.Delay(1000, ct);
Console.WriteLine($"[LastWorkAsync] complete i: {_i}");
}
private static async Task SomeWorkOneAsync(CancellationToken ct)
{
Console.WriteLine($"[SomeWorkOneAsync] start i: {_i}");
Console.WriteLine($"[SomeWorkOneAsync] await Task.Delay(1000, ct) start i: {_i}");
await Task.Delay(1000, ct);
Console.WriteLine($"[SomeWorkOneAsync] await Task.Delay(1000, ct) complete i: {_i}");
LateTaskAsync(ct);
Console.WriteLine($"[SomeWorkOneAsync] await Task.Delay(100, ct) start i: {_i}");
await Task.Delay(100, ct);
Console.WriteLine($"[SomeWorkOneAsync] await Task.Delay(100, ct) complete i: {_i}");
Console.WriteLine($"[SomeWorkOneAsync] complete i: {_i}");
}
private static async Task LateTaskAsync(CancellationToken ct)
{
Console.WriteLine($"[LateTaskAsync] start i: {_i}");
await Task.Delay(301, ct);//301 хорошо иллюстрирует баг. 100 иллюстрирует разницу между точкой создания таска и ожидания
_tokenSource.Cancel();
Console.WriteLine($"[LateTaskAsync] complete i: {_i}");
}
private static async Task SomeWorkTwoAsync(Task someWorkOneTask, CancellationToken ct)
{
Console.WriteLine($"[SomeWorkTwoAsync] start i: {_i}");
ct.ThrowIfCancellationRequested();
await someWorkOneTask;
Console.WriteLine($"[SomeWorkTwoAsync] await someWorkOneTask complete i: {_i}");
await FakeRequestAsync(ct);
Console.WriteLine($"[SomeWorkTwoAsync] complete i: {_i}");
}
private static async Task FakeRequestAsync(CancellationToken ct)
{
if (ct.IsCancellationRequested)
{
Console.WriteLine($"[FakeRequestAsync] cancelation requested i: {_i}");
ct.ThrowIfCancellationRequested();
}
await Task.Delay(100);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment