Skip to content

Latest commit

 

History

History
46 lines (38 loc) · 724 Bytes

task-programming.md

File metadata and controls

46 lines (38 loc) · 724 Bytes

task programming

sample bg task

var cts = new CancellationTokenSource();

var bgtask = Task.Run(async () =>
{
    try
    {        
        for (int i = 0; i < 10; ++i)
        {
            Console.WriteLine($"{i} working");
            await Task.Delay(1000, cts.Token);
        }
    }
    catch (OperationCanceledException)
    {
        Console.WriteLine("======> CANCELED");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"ex: {ex.Message}");
    }    
});

Console.WriteLine("RDY");
await Task.Delay(1000);
Console.WriteLine("CANCELLING");
cts.Cancel();

bgtask.Wait();

output is:

RDY
0 working
1 working
CANCELLING
======> CANCELED