Optimizing Task Code
Some checks are pending
release Linux / build (Release) (push) Waiting to run
release macOS / build (Release) (push) Waiting to run
release Windows desktop (Avalonia UI) / build (Release) (push) Waiting to run
release Windows / build (Release) (push) Waiting to run

This commit is contained in:
2dust 2025-03-03 16:57:55 +08:00
parent 837cfbd03b
commit 7fbb0013b0
4 changed files with 34 additions and 33 deletions

View file

@ -7,9 +7,9 @@ namespace ServiceLib.Handler
private static readonly Lazy<ClashApiHandler> instance = new(() => new());
public static ClashApiHandler Instance => instance.Value;
private static readonly string _tag = "ClashApiHandler";
private Dictionary<string, ProxiesItem>? _proxies;
public Dictionary<string, object> ProfileContent { get; set; }
private static readonly string _tag = "ClashApiHandler";
public async Task<Tuple<ClashProxies, ClashProviders>?> GetClashProxiesAsync(Config config)
{
@ -54,7 +54,7 @@ namespace ServiceLib.Handler
return;
}
lstProxy = new List<ClashProxyModel>();
foreach (KeyValuePair<string, ProxiesItem> kv in _proxies)
foreach (var kv in _proxies)
{
if (Global.notAllowTestType.Contains(kv.Value.type.ToLower()))
{

View file

@ -26,7 +26,7 @@ namespace ServiceLib.Handler
//Execute once 20 minute
if (numOfExecuted % 20 == 0)
{
Logging.SaveLog("Execute save config");
//Logging.SaveLog("Execute save config");
await ConfigHandler.SaveConfig(config);
await ProfileExHandler.Instance.SaveTo();
@ -35,7 +35,7 @@ namespace ServiceLib.Handler
//Execute once 1 hour
if (numOfExecuted % 60 == 0)
{
Logging.SaveLog("Execute delete expired files");
//Logging.SaveLog("Execute delete expired files");
FileManager.DeleteExpiredFiles(Utils.GetBinConfigPath(), DateTime.Now.AddHours(-1));
FileManager.DeleteExpiredFiles(Utils.GetLogPath(), DateTime.Now.AddMonths(-1));
@ -72,7 +72,7 @@ namespace ServiceLib.Handler
updateFunc?.Invoke(success, msg);
if (success)
{
Logging.SaveLog("Update subscription end" + msg);
Logging.SaveLog($"Update subscription end. {msg}");
}
});
item.UpdateTime = updateTime;

View file

@ -53,26 +53,31 @@ namespace ServiceLib.ViewModels
private async Task Init()
{
var lastTime = DateTime.Now;
Task.Run(async () =>
{
var numOfExecuted = 1;
while (true)
{
await Task.Delay(1000 * 5);
numOfExecuted++;
if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
{
continue;
}
if (_config.ClashUIItem.ConnectionsRefreshInterval <= 0)
{
continue;
}
if (numOfExecuted % _config.ClashUIItem.ConnectionsRefreshInterval != 0)
{
continue;
}
await GetClashConnections();
}
});
Observable.Interval(TimeSpan.FromSeconds(5))
.Subscribe(async x =>
{
if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
{
return;
}
var dtNow = DateTime.Now;
if (_config.ClashUIItem.ConnectionsRefreshInterval > 0)
{
if ((dtNow - lastTime).Minutes % _config.ClashUIItem.ConnectionsRefreshInterval == 0)
{
await GetClashConnections();
lastTime = dtNow;
}
Task.Delay(1000).Wait();
}
});
await Task.CompletedTask;
}

View file

@ -95,8 +95,8 @@ namespace ServiceLib.ViewModels
private async Task Init()
{
await ProxiesReload();
_ = DelayTestTask();
await ProxiesReload();
}
private async Task DoRulemodeSelected(bool c)
@ -383,8 +383,6 @@ namespace ServiceLib.ViewModels
private async Task ProxiesDelayTest(bool blAll)
{
//UpdateHandler(false, "Clash Proxies Latency Test");
ClashApiHandler.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), async (item, result) =>
{
if (item == null)
@ -434,13 +432,13 @@ namespace ServiceLib.ViewModels
public async Task DelayTestTask()
{
var lastTime = DateTime.Now;
_ = Task.Run(async () =>
Task.Run(async () =>
{
var numOfExecuted = 1;
while (true)
{
await Task.Delay(1000 * 60);
numOfExecuted++;
if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
{
continue;
@ -449,13 +447,11 @@ namespace ServiceLib.ViewModels
{
continue;
}
var dtNow = DateTime.Now;
if ((dtNow - lastTime).Minutes % _config.ClashUIItem.ProxiesAutoDelayTestInterval != 0)
if (numOfExecuted % _config.ClashUIItem.ProxiesAutoDelayTestInterval != 0)
{
continue;
}
await ProxiesDelayTest();
lastTime = dtNow;
}
});
await Task.CompletedTask;