#333, add command -delay_restart <seconds> to restart PW in hidden mode

This commit is contained in:
Kang Yu 2024-07-13 21:04:54 -07:00
parent 63334fa67c
commit b782e9b034

View file

@ -51,8 +51,8 @@ namespace PersistentWindows.SystrayShell
pwp = new PersistentWindowProcessor(); pwp = new PersistentWindowProcessor();
bool splash = true; bool splash = true;
int delay_start = 0; int delay_restart = 0;
bool relaunch = false; int relaunch_delay = 0;
int delay_manual_capture = 0; int delay_manual_capture = 0;
int delay_auto_capture = 0; int delay_auto_capture = 0;
bool redirect_appdata = false; // use "." instead of appdata/local/PersistentWindows to store db file bool redirect_appdata = false; // use "." instead of appdata/local/PersistentWindows to store db file
@ -86,13 +86,12 @@ namespace PersistentWindows.SystrayShell
pwp.haltRestore = (Int32)(float.Parse(arg) * 1000); pwp.haltRestore = (Int32)(float.Parse(arg) * 1000);
continue; continue;
} }
else if (delay_start != 0) else if (delay_restart != 0)
{ {
delay_start = 0; delay_restart = 0;
if (!waiting_taskbar) if (!waiting_taskbar)
{ {
Thread.Sleep((Int32)(float.Parse(arg) * 1000)); relaunch_delay = (Int32)(float.Parse(arg));
relaunch = true;
} }
continue; continue;
} }
@ -153,8 +152,8 @@ namespace PersistentWindows.SystrayShell
case "-enable_auto_restore_by_manual_capture": case "-enable_auto_restore_by_manual_capture":
pwp.manualNormalSession = true; pwp.manualNormalSession = true;
break; break;
case "-delay_start": case "-delay_restart":
delay_start = 1; delay_restart = 1;
break; break;
case "-wait_taskbar": case "-wait_taskbar":
waiting_taskbar = true; waiting_taskbar = true;
@ -322,9 +321,9 @@ namespace PersistentWindows.SystrayShell
systrayForm = new SystrayForm(check_upgrade); systrayForm = new SystrayForm(check_upgrade);
systrayForm.autoUpgrade = auto_upgrade; systrayForm.autoUpgrade = auto_upgrade;
if (relaunch) if (relaunch_delay > 0)
{ {
Restart(2); RestartHidden(relaunch_delay);
return; return;
} }
@ -418,6 +417,21 @@ namespace PersistentWindows.SystrayShell
Log.Error("program restarted"); Log.Error("program restarted");
} }
static void RestartHidden(int delay)
{
Process p = new Process();
string batFile = Path.Combine(AppdataFolder, $"pw_restart.bat");
string content = $"timeout /t {delay} /nobreak > NUL";
content += "\nstart \"\" /B \"" + Path.Combine(Application.StartupPath, Application.ProductName) + ".exe\" " + "-wait_taskbar " + Program.CmdArgs;
File.WriteAllText(batFile, content);
p.StartInfo.FileName = batFile;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = true;
p.Start();
Log.Error("program restarted");
}
public static void ShowRestoreTip() public static void ShowRestoreTip()
{ {
if (systrayForm.contextMenuStripSysTray.InvokeRequired) if (systrayForm.contextMenuStripSysTray.InvokeRequired)