Brad Firesheets
Brad Firesheets

Reputation: 812

Run 2 tasks in parallel and cancel the 2nd task if the first completes .NET

If have 2 methods I want to run in parallel on different threads, let's call them Task1 and Task2. If Task2 completes before Task1 then I want the results of both combined into a list. If Task1 completes and Task2 is still running, it should cancel Task2 to avoid further processing. WhenAll doesn't work because it will wait for both tasks. WhenAny doesn't work because it will return if either finish first.

Upvotes: 1

Views: 1790

Answers (1)

Brad Firesheets
Brad Firesheets

Reputation: 812

This is the solution I came up with.

static void Main(string[] args)
{
    var tokenSource = new CancellationTokenSource();
    var token = tokenSource.Token;
    var rates = new List<string>();

    try
    {
        var p44Task = Task.Factory.StartNew(() => GetP44Rates(token), token);

        var mgRates = await GetMGRates();

        rates.AddRange(mgRates);

        if (p44Task.IsCompleted && p44Task.Result.IsCompleted)
        {
            rates.AddRange(p44Task.Result.Result);
        }
        else
        {
            // Cancel the p44 task
            tokenSource.Cancel();
        }
    }
    catch (Exception e)
    {
        tokenSource.Cancel();
        Console.WriteLine(e);
    }

    foreach (var rate in rates)
    {
        Console.WriteLine(rate);
    }

    Console.ReadLine();

}

private static async Task<List<string>> GetMGRates(CancellationToken cancellationToken)
{
    var rates = new List<string>();

    for (var i = 0; i < 10; i++)
    {
        rates.Add($"MG: {i}");
        Console.WriteLine($"MG inside {i}");
        await Task.Delay(1000);
    }


    return rates;
}

private static async Task<List<string>> GetP44Rates(CancellationToken ct)
{
    var rates = new List<string>();

    for (var i = 0; i < 50; i++)
    {
        rates.Add($"P44: {i}");
        Console.WriteLine($"P44: {i}");
        await Task.Delay(1000);

        if (ct.IsCancellationRequested)
        {
            Console.WriteLine("bye from p44.");
            Console.WriteLine("\nPress Enter to quit.");

            ct.ThrowIfCancellationRequested();
        }
    }

    return rates;
}

Upvotes: 1

Related Questions