This commit is contained in:
qianjiawei
2023-11-03 09:54:04 +08:00
48 changed files with 623 additions and 204 deletions

View File

@@ -25,6 +25,21 @@ namespace Tnb.Common.Extension
return tcs.Task;
}
public static async Task<T> Retry<T>(Func<Task<T>> task, int retries,
TimeSpan delay, CancellationToken cts = default) =>
await task().ContinueWith(async innerTask =>
{
cts.ThrowIfCancellationRequested();
if (innerTask.Status != TaskStatus.Faulted)
return innerTask.Result;
if (retries == 0)
throw innerTask.Exception ?? throw new Exception();
await Task.Delay(delay, cts);
return await Retry(task, retries - 1, delay, cts);
}).Unwrap();
public static async Task Catch(this Task task, Func<Exception,Task> exceptionHandler)
{
try

View File

@@ -1,5 +1,6 @@
using System.Text.RegularExpressions;
using JNPF.Common.Extension;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace System;
@@ -183,4 +184,17 @@ public static class StringExtensions
public static string ToCamel(this string str) => str.SplitWord().Select((a, i) => i == 0 ? a : a.UpperFirst()).JoinAsString("");
#endregion
public static (string, string) LastSplit(this string str, char seperate)
{
int n = str.LastIndexOf(seperate);
if(n > 0)
{
return (str.Substring(0, n), str.Substring(n + 1));
}
else
{
return (str, null);
}
}
}