11using System ;
22using System . Collections . Generic ;
3+ using System . Diagnostics ;
34using System . Linq ;
5+ using System . Threading . Tasks ;
46using System . Windows ;
57using System . Windows . Forms ;
68using CommunityToolkit . Mvvm . Input ;
911using Flow . Launcher . Core . Resource ;
1012using Flow . Launcher . Helper ;
1113using Flow . Launcher . Infrastructure ;
14+ using Flow . Launcher . Infrastructure . Image ;
1215using Flow . Launcher . Infrastructure . UserSettings ;
1316using Flow . Launcher . Plugin ;
1417using Flow . Launcher . Plugin . SharedModels ;
@@ -75,7 +78,7 @@ public bool StartFlowLauncherOnSystemStartup
7578 // even if we encounter an error while setting the startup method
7679 if ( value && UseLogonTaskForStartup )
7780 {
78- CheckAdminChangeAndAskForRestart ( ) ;
81+ _ = CheckAdminChangeAndAskForRestartAsync ( ) ;
7982 }
8083 }
8184 }
@@ -112,7 +115,7 @@ public bool UseLogonTaskForStartup
112115 // even if we encounter an error while setting the startup method
113116 if ( StartFlowLauncherOnSystemStartup && value )
114117 {
115- CheckAdminChangeAndAskForRestart ( ) ;
118+ _ = CheckAdminChangeAndAskForRestartAsync ( ) ;
116119 }
117120 }
118121 }
@@ -139,22 +142,41 @@ public bool AlwaysRunAsAdministrator
139142
140143 // If we have enabled logon task startup, we need to check if we need to restart the app
141144 // even if we encounter an error while setting the startup method
142- CheckAdminChangeAndAskForRestart ( ) ;
145+ _ = CheckAdminChangeAndAskForRestartAsync ( ) ;
143146 }
144147 }
145148 }
146149
147- private void CheckAdminChangeAndAskForRestart ( )
150+ private async Task CheckAdminChangeAndAskForRestartAsync ( )
148151 {
149- if ( ( AlwaysRunAsAdministrator && ! _isAdministrator ) || // Change from non-admin to admin
150- ( ! AlwaysRunAsAdministrator && _isAdministrator ) ) // Change from admin to non-admin
152+ // When we change from non-admin to admin, we need to restart the app as administrator to apply the changes
153+ // Under non-administrator, we cannot delete or set the logon task which is run as administrator
154+ if ( AlwaysRunAsAdministrator && ! _isAdministrator )
151155 {
152156 if ( App . API . ShowMsgBox (
153157 App . API . GetTranslation ( "runAsAdministratorChangeAndRestart" ) ,
154158 App . API . GetTranslation ( "runAsAdministratorChange" ) ,
155159 MessageBoxButton . YesNo ) == MessageBoxResult . Yes )
156160 {
157- App . API . RestartApp ( AlwaysRunAsAdministrator ? "runas" : string . Empty ) ;
161+ App . API . HideMainWindow ( ) ;
162+
163+ // We must manually save because of Environment.Exit(0)
164+ // which will cause ungraceful exit
165+ App . API . SaveAppAllSettings ( ) ;
166+
167+ // Wait for all image caches to be saved before restarting
168+ await ImageLoader . WaitSaveAsync ( ) ;
169+
170+ // Restart the app as administrator
171+ var startInfo = new ProcessStartInfo
172+ {
173+ UseShellExecute = true ,
174+ WorkingDirectory = Environment . CurrentDirectory ,
175+ FileName = Constant . ExecutablePath ,
176+ Verb = "runas"
177+ } ;
178+ Process . Start ( startInfo ) ;
179+ Environment . Exit ( 0 ) ;
158180 }
159181 }
160182 }
0 commit comments