mirror of
https://github.com/2dust/v2rayN.git
synced 2025-04-19 21:52:25 +00:00
Optimizing Task Code
This commit is contained in:
parent
837cfbd03b
commit
7fbb0013b0
4 changed files with 34 additions and 33 deletions
|
@ -7,9 +7,9 @@ namespace ServiceLib.Handler
|
||||||
private static readonly Lazy<ClashApiHandler> instance = new(() => new());
|
private static readonly Lazy<ClashApiHandler> instance = new(() => new());
|
||||||
public static ClashApiHandler Instance => instance.Value;
|
public static ClashApiHandler Instance => instance.Value;
|
||||||
|
|
||||||
|
private static readonly string _tag = "ClashApiHandler";
|
||||||
private Dictionary<string, ProxiesItem>? _proxies;
|
private Dictionary<string, ProxiesItem>? _proxies;
|
||||||
public Dictionary<string, object> ProfileContent { get; set; }
|
public Dictionary<string, object> ProfileContent { get; set; }
|
||||||
private static readonly string _tag = "ClashApiHandler";
|
|
||||||
|
|
||||||
public async Task<Tuple<ClashProxies, ClashProviders>?> GetClashProxiesAsync(Config config)
|
public async Task<Tuple<ClashProxies, ClashProviders>?> GetClashProxiesAsync(Config config)
|
||||||
{
|
{
|
||||||
|
@ -54,7 +54,7 @@ namespace ServiceLib.Handler
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
lstProxy = new List<ClashProxyModel>();
|
lstProxy = new List<ClashProxyModel>();
|
||||||
foreach (KeyValuePair<string, ProxiesItem> kv in _proxies)
|
foreach (var kv in _proxies)
|
||||||
{
|
{
|
||||||
if (Global.notAllowTestType.Contains(kv.Value.type.ToLower()))
|
if (Global.notAllowTestType.Contains(kv.Value.type.ToLower()))
|
||||||
{
|
{
|
||||||
|
|
|
@ -26,7 +26,7 @@ namespace ServiceLib.Handler
|
||||||
//Execute once 20 minute
|
//Execute once 20 minute
|
||||||
if (numOfExecuted % 20 == 0)
|
if (numOfExecuted % 20 == 0)
|
||||||
{
|
{
|
||||||
Logging.SaveLog("Execute save config");
|
//Logging.SaveLog("Execute save config");
|
||||||
|
|
||||||
await ConfigHandler.SaveConfig(config);
|
await ConfigHandler.SaveConfig(config);
|
||||||
await ProfileExHandler.Instance.SaveTo();
|
await ProfileExHandler.Instance.SaveTo();
|
||||||
|
@ -35,7 +35,7 @@ namespace ServiceLib.Handler
|
||||||
//Execute once 1 hour
|
//Execute once 1 hour
|
||||||
if (numOfExecuted % 60 == 0)
|
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.GetBinConfigPath(), DateTime.Now.AddHours(-1));
|
||||||
FileManager.DeleteExpiredFiles(Utils.GetLogPath(), DateTime.Now.AddMonths(-1));
|
FileManager.DeleteExpiredFiles(Utils.GetLogPath(), DateTime.Now.AddMonths(-1));
|
||||||
|
@ -72,7 +72,7 @@ namespace ServiceLib.Handler
|
||||||
updateFunc?.Invoke(success, msg);
|
updateFunc?.Invoke(success, msg);
|
||||||
if (success)
|
if (success)
|
||||||
{
|
{
|
||||||
Logging.SaveLog("Update subscription end" + msg);
|
Logging.SaveLog($"Update subscription end. {msg}");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
item.UpdateTime = updateTime;
|
item.UpdateTime = updateTime;
|
||||||
|
|
|
@ -53,26 +53,31 @@ namespace ServiceLib.ViewModels
|
||||||
|
|
||||||
private async Task Init()
|
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;
|
await Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -95,8 +95,8 @@ namespace ServiceLib.ViewModels
|
||||||
|
|
||||||
private async Task Init()
|
private async Task Init()
|
||||||
{
|
{
|
||||||
await ProxiesReload();
|
|
||||||
_ = DelayTestTask();
|
_ = DelayTestTask();
|
||||||
|
await ProxiesReload();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task DoRulemodeSelected(bool c)
|
private async Task DoRulemodeSelected(bool c)
|
||||||
|
@ -383,8 +383,6 @@ namespace ServiceLib.ViewModels
|
||||||
|
|
||||||
private async Task ProxiesDelayTest(bool blAll)
|
private async Task ProxiesDelayTest(bool blAll)
|
||||||
{
|
{
|
||||||
//UpdateHandler(false, "Clash Proxies Latency Test");
|
|
||||||
|
|
||||||
ClashApiHandler.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), async (item, result) =>
|
ClashApiHandler.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), async (item, result) =>
|
||||||
{
|
{
|
||||||
if (item == null)
|
if (item == null)
|
||||||
|
@ -434,13 +432,13 @@ namespace ServiceLib.ViewModels
|
||||||
|
|
||||||
public async Task DelayTestTask()
|
public async Task DelayTestTask()
|
||||||
{
|
{
|
||||||
var lastTime = DateTime.Now;
|
Task.Run(async () =>
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
{
|
||||||
|
var numOfExecuted = 1;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
await Task.Delay(1000 * 60);
|
await Task.Delay(1000 * 60);
|
||||||
|
numOfExecuted++;
|
||||||
if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
|
if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
@ -449,13 +447,11 @@ namespace ServiceLib.ViewModels
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
var dtNow = DateTime.Now;
|
if (numOfExecuted % _config.ClashUIItem.ProxiesAutoDelayTestInterval != 0)
|
||||||
if ((dtNow - lastTime).Minutes % _config.ClashUIItem.ProxiesAutoDelayTestInterval != 0)
|
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
await ProxiesDelayTest();
|
await ProxiesDelayTest();
|
||||||
lastTime = dtNow;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await Task.CompletedTask;
|
await Task.CompletedTask;
|
||||||
|
|
Loading…
Reference in a new issue