microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.4

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Common/Extensions/TaskExtensions.cs

29lines · modepreview

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Teams.Common.Extensions;

public static class TaskExtensions
{
    public static async Task<T> Retry<T>(Func<Task<T>> taskFactory, int max = 3, int delay = 200)
    {
        try
        {
            return await taskFactory().ConfigureAwait(false);
        }
        // don't retry cancelled
        catch (OperationCanceledException) when (max > 0)
        {
            throw;
        }
        catch (Exception ex)
        {
            if (max > 0)
            {
                await Task.Delay(delay);
                return await Retry(taskFactory, max - 1, delay * 2).ConfigureAwait(false);
            }
            throw new Exception(ex.Message, ex);
        }
    }
}