@@ -115,13 +115,25 @@ public async Task CheckAndDownloadLatestVersion(Download Download)
115115 {
116116 if ( localVersionModel == null || localVersionModel . Version != CurrentVersion . Version )
117117 {
118+ Download . Active = true ;
119+ Download . DownloadState = DownloadState . InProgress ;
120+ StopTTS ( ) ;
121+ await Task . Delay ( 2 * 1000 ) ; //to give it some grace period to shutdown
122+
118123 var LocalVersion = System . IO . Path . Combine ( installationBasePath , "tts" ) ;
119124 if ( System . IO . Directory . Exists ( LocalVersion ) )
120125 {
121- System . IO . Directory . Delete ( LocalVersion , true ) ;
126+ foreach ( var file in System . IO . Directory . GetFiles ( LocalVersion ) )
127+ {
128+ System . IO . File . Delete ( file ) ;
129+ }
130+
131+ foreach ( var directory in System . IO . Directory . GetDirectories ( LocalVersion ) )
132+ {
133+ System . IO . Directory . Delete ( directory , true ) ;
134+ }
122135 }
123136
124-
125137 var zip = _environmentService . GetCurrentEnvironment ( ) switch
126138 {
127139 CurrentEnvironment . WindowsStandalone => "win.zip" ,
@@ -130,8 +142,7 @@ public async Task CheckAndDownloadLatestVersion(Download Download)
130142 _ => null
131143 } ;
132144
133- Download . Active = true ;
134- Download . DownloadState = DownloadState . InProgress ;
145+
135146
136147 HttpResponseMessage request = await _httpClient . GetAsync ( ApiUrls . TTSFiles + "/" + zip ,
137148 HttpCompletionOption . ResponseHeadersRead ) ;
@@ -166,14 +177,12 @@ private void ExtractTo(Stream progressStream, string LocalVersion, Download Down
166177 case CurrentEnvironment . WindowsStandalone :
167178 {
168179 ZipArchive archive = new ( progressStream ) ;
169- Download . DownloadState = DownloadState . Extracting ;
170180 archive . ExtractToDirectory ( LocalVersion , true ) ;
171181 break ;
172182 }
173183 case CurrentEnvironment . LinuxStandalone or CurrentEnvironment . LinuxFlatpak :
174184 {
175185 using var decompressedStream = DecompressXz ( progressStream ) ; // Decompress XZ stream to get .tar
176-
177186 ExtractTar ( decompressedStream , LocalVersion ) ;
178187 break ;
179188 }
@@ -183,6 +192,7 @@ private void ExtractTo(Stream progressStream, string LocalVersion, Download Down
183192
184193 Download . Active = false ;
185194 Download . DownloadState = DownloadState . InProgress ;
195+ StartTTS ( ) ;
186196 }
187197
188198 private static Stream DecompressXz ( Stream compressedStream )
@@ -250,11 +260,19 @@ public void StartTTS()
250260 var Preference = _preferencesService . GetPreferences ( ) ;
251261 if ( ( Preference . TTSEnabled is true ) == false ) return ;
252262
253- if ( process != null && process . HasExited == false )
263+ try
254264 {
255- return ;
265+ if ( process != null && process . HasExited == false )
266+ {
267+ return ;
268+ }
269+ }
270+ catch ( Exception e )
271+ {
272+ Log . Error ( e . ToString ( ) ) ;
256273 }
257274
275+
258276 string installationBasePath = _preferencesService . GetPreferences ( ) . InstallationPath ;
259277 var LocalVersion = System . IO . Path . Combine ( installationBasePath , "tts" ) ;
260278 if ( System . IO . Directory . Exists ( LocalVersion ) == false )
@@ -303,10 +321,14 @@ public void StartTTS()
303321 // Ensure subprocess ends when the main application exits
304322 AppDomain . CurrentDomain . ProcessExit += ( sender , e ) =>
305323 {
306- if ( process . HasExited == false )
324+ if ( process != null )
307325 {
308- process . Kill ( ) ;
326+ if ( process . HasExited == false )
327+ {
328+ process . Kill ( ) ;
329+ }
309330 }
331+
310332 } ;
311333
312334 try
@@ -324,10 +346,18 @@ public void StopTTS()
324346 {
325347 if ( process != null )
326348 {
327- if ( process . HasExited == false )
349+ try
350+ {
351+ if ( process . HasExited == false )
352+ {
353+ process . Kill ( ) ;
354+ }
355+ }
356+ catch ( Exception e )
328357 {
329- process . Kill ( ) ;
358+ Log . Error ( e . ToString ( ) ) ;
330359 }
360+
331361 }
332362 }
333363}
0 commit comments