Compare commits

..

2 Commits

Author SHA1 Message Date
2dust
e20c11c1a7 Refactor reload logic with semaphore for concurrency 2025-11-08 20:48:55 +08:00
2dust
a6af95e083 Bug fix
https://github.com/2dust/v2rayN/issues/8276
2025-11-08 20:10:20 +08:00

View File

@@ -64,8 +64,6 @@ public class MainWindowViewModel : MyReactiveObject
#endregion Menu #endregion Menu
private bool _hasNextReloadJob = false;
#region Init #region Init
public MainWindowViewModel(Func<EViewAction, object?, Task<bool>>? updateView) public MainWindowViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
@@ -268,7 +266,6 @@ public class MainWindowViewModel : MyReactiveObject
} }
await RefreshServers(); await RefreshServers();
SetReloadEnabled(true);
await Reload(); await Reload();
} }
@@ -525,15 +522,20 @@ public class MainWindowViewModel : MyReactiveObject
#region core job #region core job
private bool _hasNextReloadJob = false;
private readonly SemaphoreSlim _reloadSemaphore = new(1, 1);
public async Task Reload() public async Task Reload()
{ {
//If there are unfinished reload job, marked with next job. //If there are unfinished reload job, marked with next job.
if (!BlReloadEnabled) if (!await _reloadSemaphore.WaitAsync(0))
{ {
_hasNextReloadJob = true; _hasNextReloadJob = true;
return; return;
} }
try
{
SetReloadEnabled(false); SetReloadEnabled(false);
var msgs = await ActionPrecheckManager.Instance.Check(_config.IndexId); var msgs = await ActionPrecheckManager.Instance.Check(_config.IndexId);
@@ -544,7 +546,6 @@ public class MainWindowViewModel : MyReactiveObject
NoticeManager.Instance.SendMessage(msg); NoticeManager.Instance.SendMessage(msg);
} }
NoticeManager.Instance.Enqueue(Utils.List2String(msgs.Take(10).ToList(), true)); NoticeManager.Instance.Enqueue(Utils.List2String(msgs.Take(10).ToList(), true));
SetReloadEnabled(true);
return; return;
} }
@@ -563,13 +564,19 @@ public class MainWindowViewModel : MyReactiveObject
} }
ReloadResult(showClashUI); ReloadResult(showClashUI);
}
finally
{
SetReloadEnabled(true); SetReloadEnabled(true);
_reloadSemaphore.Release();
//If there is a next reload job, execute it.
if (_hasNextReloadJob) if (_hasNextReloadJob)
{ {
_hasNextReloadJob = false; _hasNextReloadJob = false;
await Reload(); await Reload();
} }
} }
}
private void ReloadResult(bool showClashUI) private void ReloadResult(bool showClashUI)
{ {