From 4ad4e27dc102088c42b613e484b10ad745b6fdd5 Mon Sep 17 00:00:00 2001
From: 2dust <31833384+2dust@users.noreply.github.com>
Date: Tue, 14 Jan 2025 17:05:13 +0800
Subject: [PATCH] Improve reload function

---
 .../ViewModels/MainWindowViewModel.cs         | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
index b4542b2a..d65a516f 100644
--- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
+++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
@@ -63,6 +63,8 @@ namespace ServiceLib.ViewModels
 
         #endregion Menu
 
+        private bool _hasNextReloadJob = false;
+
         #region Init
 
         public MainWindowViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
@@ -534,14 +536,12 @@ namespace ServiceLib.ViewModels
 
         #region core job
 
-        public async Task Reload(int times = 0)
+        public async Task Reload()
         {
-            //If there are unfinished reload job, wait a few seconds and exec again.
+            //If there are unfinished reload job, marked with next job.
             if (!BlReloadEnabled)
             {
-                await Task.Delay(3000);
-                if (times > 3) return;
-                await Reload(++times);
+                _hasNextReloadJob = true;
                 return;
             }
 
@@ -552,12 +552,19 @@ namespace ServiceLib.ViewModels
             await SysProxyHandler.UpdateSysProxy(_config, false);
 
             _updateView?.Invoke(EViewAction.DispatcherReload, null);
+
+            BlReloadEnabled = true;
+            if (_hasNextReloadJob)
+            {
+                _hasNextReloadJob = false;
+                await Reload();
+            }
         }
 
         public void ReloadResult()
         {
+            // BlReloadEnabled = true;
             //Locator.Current.GetService<StatusBarViewModel>()?.ChangeSystemProxyAsync(_config.systemProxyItem.sysProxyType, false);
-            BlReloadEnabled = true;
             ShowClashUI = _config.IsRunningCore(ECoreType.sing_box);
             if (ShowClashUI)
             {