@@ -70,7 +70,7 @@ public FileSwapper(AppEntry appEntry, PreferencesXML preferencesXML)
70
70
71
71
public async Task InitiateFileSwaps ( PowerLineStatus forPowerLineStatus , PreferencesXML preferencesXML )
72
72
{
73
- Logger . inst . Log ( $ "Initiating FileSwap for AppEntry with target path { AppEntry . AppPath } ") ;
73
+ Logger . inst . Log ( $ "Initiating File Swaps for AppEntry with target path { AppEntry . AppPath } ") ;
74
74
75
75
//if the AppEntry doesn't yet have its own FileSwap folder, create it
76
76
bool appEntryFolderExists = Directory . Exists ( SettingsBankFolderPath ) ;
@@ -80,6 +80,7 @@ public async Task InitiateFileSwaps(PowerLineStatus forPowerLineStatus, Preferen
80
80
File . Create ( Path . Combine ( SettingsBankFolderPath , $ "{ AppEntry . AppName } settings are in { SettingsBankFolderPath } ") ) ;
81
81
}
82
82
83
+ List < Task > fileSwapTasks = new ( ) ;
83
84
//commence the swap for each FileSwapPath
84
85
for ( int i = 0 ; i < AppEntry . FileSwapperPaths . Length ; i ++ )
85
86
{
@@ -99,7 +100,7 @@ public async Task InitiateFileSwaps(PowerLineStatus forPowerLineStatus, Preferen
99
100
100
101
try
101
102
{
102
- Task singleFileSwap = InitiateSingleFileSwap ( current , i ) ;
103
+ fileSwapTasks . Add ( InitiateSingleFileSwap ( current , i ) ) ;
103
104
}
104
105
catch ( AggregateException )
105
106
{
@@ -108,7 +109,11 @@ public async Task InitiateFileSwaps(PowerLineStatus forPowerLineStatus, Preferen
108
109
109
110
} //...repeat for every SwapPath
110
111
111
- Logger . inst . Log ( $ "Finished firing FileSwap logic for AppEntry with target path { AppEntry . AppPath } ") ;
112
+ Logger . inst . Log ( $ "Finished firing single FileSwap tasks for AppEntry with target path { AppEntry . AppPath } ") ;
113
+
114
+ await Task . WhenAll ( fileSwapTasks ) ;
115
+
116
+ Logger . inst . Log ( $ "Completed all FileSwap tasks for AppEntry with target path { AppEntry . AppPath } ") ;
112
117
113
118
}
114
119
@@ -154,15 +159,15 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int
154
159
WaitForOlderFileSwaps :
155
160
if ( OngoingFileSwapTasks . Any ( x => x . FileSwapPath == fileSwapPathTask . FileSwapPath && x . ID < fileSwapPathTask . ID ) )
156
161
{
157
- Task log = Logger . inst . Log ( $ "An older FileSwap task for { swapPath } already exists, delaying") ;
162
+ Logger . inst . Log ( $ "An older FileSwap task for { swapPath } already exists, delaying") ;
158
163
await Task . Delay ( 10000 ) ;
159
164
goto WaitForOlderFileSwaps ;
160
165
}
161
166
162
167
WaitForAppToClose :
163
- if ( IsProcessRunning ( AppEntry . AppPath ) )
168
+ if ( await IsProcessRunning ( AppEntry . AppPath ) ) //IsProcessRunning is apparently VERY expensive
164
169
{
165
- Task log = Logger . inst . Log ( $ "Target app { AppEntry . AppPath } is still running, delaying File Swap for { swapPath } ") ;
170
+ Logger . inst . Log ( $ "Target app { AppEntry . AppPath } is still running, delaying File Swap for { swapPath } ") ;
166
171
await Task . Delay ( 5000 ) ;
167
172
goto WaitForAppToClose ;
168
173
}
@@ -184,7 +189,7 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int
184
189
StoreFile_OnBattery :
185
190
try
186
191
{
187
- FileCopyConsiderAccessRules ( swapPath , onBatteryStoredFilePath , true , false ) ;
192
+ await FileCopyConsiderAccessRules ( swapPath , onBatteryStoredFilePath , true , false ) ;
188
193
File . Create ( Path . Combine ( SettingsBankFolderPath , $ "{ Path . GetFileName ( swapPath ) } is in { swapPathHash } ") ) ;
189
194
}
190
195
catch ( IOException )
@@ -203,7 +208,7 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int
203
208
StoreFile_PluggedIn :
204
209
try
205
210
{
206
- FileCopyConsiderAccessRules ( swapPath , pluggedInStoredFilePath , true , false ) ;
211
+ await FileCopyConsiderAccessRules ( swapPath , pluggedInStoredFilePath , true , false ) ;
207
212
File . Create ( Path . Combine ( SettingsBankFolderPath , $ "{ Path . GetFileName ( swapPath ) } is in { swapPathHash } ") ) ;
208
213
}
209
214
catch ( IOException )
@@ -224,13 +229,13 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int
224
229
try
225
230
{
226
231
//no need to cancel the save if the substitution fails
227
- FileCopyConsiderAccessRules ( swapPath , pluggedInStoredFilePath , true , false ) ; //save current config to PluggedIn store
232
+ await FileCopyConsiderAccessRules ( swapPath , pluggedInStoredFilePath , true , false ) ; //save current config to PluggedIn store
228
233
string s1 = $ "Saved SwapPath { swapPath } for app { AppEntry . AppPath } as PluggedIn";
229
234
Logger . inst . Log ( s1 ) ;
230
235
231
236
errorFlag = 1 ;
232
237
233
- FileCopyConsiderAccessRules ( onBatteryStoredFilePath , swapPath , true , true ) ; //then substitute with OnBattery config
238
+ await FileCopyConsiderAccessRules ( onBatteryStoredFilePath , swapPath , true , true ) ; //then substitute with OnBattery config
234
239
string s2 = $ "Substituted OnBattery config into SwapPath { swapPath } for app { AppEntry . AppPath } ";
235
240
Logger . inst . Log ( s2 ) ;
236
241
@@ -256,6 +261,9 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int
256
261
preferencesXML . ModifyAppEntryAndSave ( AppEntry . AppPath , modified ) ;
257
262
string s3 = $ "Saved SwapPath state for SwapPath { swapPath } for app { AppEntry . AppPath } ";
258
263
Logger . inst . Log ( s3 ) ;
264
+ } else
265
+ {
266
+ Logger . inst . Log ( $ "Skipped operations for File Swap path { swapPath } because it's already in its correct state.") ;
259
267
}
260
268
}
261
269
else if ( forPowerLineStatus == PowerLineStatus . Online )
@@ -268,13 +276,13 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int
268
276
{
269
277
270
278
//no need to cancel the save if the substitution fails
271
- FileCopyConsiderAccessRules ( swapPath , onBatteryStoredFilePath , true , false ) ; //save current config to OnBattery store
279
+ await FileCopyConsiderAccessRules ( swapPath , onBatteryStoredFilePath , true , false ) ; //save current config to OnBattery store
272
280
string s1 = $ "Saved SwapPath { swapPath } for app { AppEntry . AppPath } as OnBattery";
273
281
Logger . inst . Log ( s1 ) ;
274
282
275
283
errorFlag = 1 ;
276
284
277
- FileCopyConsiderAccessRules ( pluggedInStoredFilePath , swapPath , true , true ) ; //then substitute with PluggedIn config
285
+ await FileCopyConsiderAccessRules ( pluggedInStoredFilePath , swapPath , true , true ) ; //then substitute with PluggedIn config
278
286
string s2 = $ "Substituted PluggedIn config into SwapPath { swapPath } for app { AppEntry . AppPath } ";
279
287
Logger . inst . Log ( s2 ) ;
280
288
}
@@ -298,20 +306,23 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int
298
306
preferencesXML . ModifyAppEntryAndSave ( AppEntry . AppPath , modified ) ;
299
307
string s3 = $ "Saved SwapPath state for SwapPath { swapPath } for app { AppEntry . AppPath } ";
300
308
Logger . inst . Log ( s3 ) ;
309
+ } else
310
+ {
311
+ Logger . inst . Log ( $ "Skipped operations for File Swap path { swapPath } because it's already in its correct state.") ;
301
312
}
302
313
}
303
314
else
304
315
{
305
316
Debug . WriteLine ( $ "Unknown power state: " + forPowerLineStatus . ToString ( ) ) ;
306
317
}
307
318
308
- await Logger . inst . Log ( $ "FileSwaps Task finished for { AppEntry . AppPath } ") ;
319
+ Logger . inst . Log ( $ "FileSwap Task { fileSwapPathTask . FileSwapPath } finished for { AppEntry . AppPath } ") ;
309
320
310
321
OngoingFileSwapTasks . Remove ( fileSwapPathTask ) ;
311
322
312
323
}
313
324
314
- static bool IsProcessRunning ( string processPath )
325
+ static async Task < bool > IsProcessRunning ( string processPath ) //this function is apparently VERY expensive to run (many mores times than this app itself) so await it!
315
326
{
316
327
Process [ ] processes = Process . GetProcesses ( ) ;
317
328
@@ -320,7 +331,7 @@ static bool IsProcessRunning(string processPath)
320
331
try
321
332
{
322
333
// Compare the process's main module file name with the specified process path
323
- if ( string . Equals ( process . MainModule . FileName , processPath , StringComparison . OrdinalIgnoreCase ) )
334
+ if ( await Task . Run ( ( ) => string . Equals ( process . MainModule . FileName , processPath , StringComparison . OrdinalIgnoreCase ) ) ) //the expensive line in question
324
335
{
325
336
return true ;
326
337
}
@@ -379,7 +390,7 @@ static byte[] GetHash(string str)
379
390
/// </summary>
380
391
///
381
392
/// And yes, I think it's clearer to throw exceptions instead of returning false, unless there is a better way (in which I have tried TWICE; this is perfectly functional)
382
- public static void FileCopyConsiderAccessRules ( string sourceFileName , string destinationFileName , bool overwrite , bool ensureDestinationExists = true )
393
+ public static Task FileCopyConsiderAccessRules ( string sourceFileName , string destinationFileName , bool overwrite , bool ensureDestinationExists = true )
383
394
{
384
395
385
396
FileInfo sourceInfo = new FileInfo ( sourceFileName ) ;
@@ -437,6 +448,8 @@ public static void FileCopyConsiderAccessRules(string sourceFileName, string des
437
448
sourceFileSecurity . SetAccessRuleProtection ( true , true ) ; //supposedly this doesn't change the existing file's security settings?
438
449
File . Copy ( sourceFileName , destinationFileName , true ) ;
439
450
destinationInfo . SetAccessControl ( sourceFileSecurity ) ; //requires the file to exist, duh
451
+
452
+ return Task . CompletedTask ;
440
453
}
441
454
442
455
/*
0 commit comments