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