public static async Task<long> CalculateSumAsync(int n, CancellationToken token, IProgress<(int, long)>? progress = null)
{
if (n <= 0)
{
throw new ArgumentOutOfRangeException(nameof(n));
}
long result = 0;
var res = await Task.Run(() =>
{
for (var i = 1; i <= n; i++)
{
result += i;
token.ThrowIfCancellationRequested();
progress?.Report((i, result));
}
return result;
});
return res;
}
public static async Task<long> CalculateSumAsync(int n, CancellationToken token, IProgress<(int, long)>? progress = null)
{
if (n <= 0)
{
throw new ArgumentOutOfRangeException(nameof(n));
}
long result = 0;
var res = await Task.Run(() =>
{
for (var i = 1; i <= n; i++)
{
result += i;
token.ThrowIfCancellationRequested();
progress?.Report((i, result));
}
return result;
});
return res;
}