17
17
using System . Text ;
18
18
using System . Threading ;
19
19
using System . Threading . Tasks ;
20
+ using System . Windows . Forms ;
20
21
using Microsoft . Extensions . Configuration ;
21
22
using myoddweb . directorywatcher ;
22
23
using myoddweb . directorywatcher . interfaces ;
24
+ using Nito . AsyncEx ;
23
25
24
26
namespace AsyncToSyncCodeRoundtripSynchroniserMonitor
25
27
{
@@ -45,7 +47,7 @@ internal static class Global
45
47
46
48
47
49
48
- internal static readonly AsyncLockQueueDictionary FileOperationLocks = new AsyncLockQueueDictionary ( ) ;
50
+ internal static readonly AsyncLockQueueDictionary < string > FileOperationLocks = new AsyncLockQueueDictionary < string > ( ) ;
49
51
//internal static readonly AsyncLock FileOperationAsyncLock = new AsyncLock();
50
52
}
51
53
#pragma warning restore S2223
@@ -227,7 +229,7 @@ await ConsoleWatch.OnAddedAsync
227
229
228
230
229
231
//listen for the Ctrl+C
230
- WaitForCtrlC ( ) ;
232
+ await WaitForCtrlC ( ) ;
231
233
232
234
Console . WriteLine ( "Stopping..." ) ;
233
235
@@ -241,7 +243,7 @@ await ConsoleWatch.OnAddedAsync
241
243
}
242
244
catch ( Exception ex )
243
245
{
244
- WriteException ( ex ) ;
246
+ await WriteException ( ex ) ;
245
247
}
246
248
}
247
249
@@ -306,36 +308,75 @@ private static IEnumerable<FileInfo> ProcessSubDirs(DirectoryInfo srcDirInfo, st
306
308
} //foreach (var dirInfo in dirInfos)
307
309
#endif
308
310
} //private static IEnumerable<FileInfo> ProcessSubDirs(DirectoryInfo srcDirInfo, string searchPattern, bool forHistory, int recursionLevel = 0)
309
- private static void WriteException ( Exception ex )
311
+ private static async Task WriteException ( Exception ex )
310
312
{
311
313
if ( ex is AggregateException aggex )
312
314
{
313
- WriteException ( aggex . InnerException ) ;
315
+ await WriteException ( aggex . InnerException ) ;
314
316
foreach ( var aggexInner in aggex . InnerExceptions )
315
317
{
316
- WriteException ( aggexInner ) ;
318
+ await WriteException ( aggexInner ) ;
317
319
}
318
320
return ;
319
321
}
320
322
321
- Console . WriteLine ( ex . Message ) ;
323
+ //Console.WriteLine(ex.Message);
324
+ StringBuilder message = new StringBuilder ( ex . Message ) ;
322
325
while ( ex . InnerException != null )
323
326
{
324
327
ex = ex . InnerException ;
325
- Console . WriteLine ( ex . Message ) ;
328
+ //Console.WriteLine(ex.Message);
329
+ message . Append ( Environment . NewLine + ex . Message ) ;
326
330
}
331
+
332
+
333
+ var time = DateTime . Now ;
334
+ var msg = $ "[{ time : yyyy.MM.dd HH:mm:ss.ffff} ]:{ message } ";
335
+ await AddMessage ( ConsoleColor . Red , msg , time , showAlert : true ) ;
336
+ }
337
+
338
+ private static async Task AddMessage ( ConsoleColor color , string message , DateTime time , bool showAlert = false )
339
+ {
340
+ await Task . Run ( ( ) =>
341
+ {
342
+ lock ( ConsoleWatch . Lock )
343
+ {
344
+ try
345
+ {
346
+ Console . ForegroundColor = color ;
347
+ Console . WriteLine ( message ) ;
348
+
349
+ if (
350
+ showAlert
351
+ && ( ConsoleWatch . PrevAlertTime != time || ConsoleWatch . PrevAlertMessage != message )
352
+ )
353
+ {
354
+ MessageBox . Show ( message , "AsyncToSyncCodeRoundtripSynchroniserMonitor" ) ;
355
+ }
356
+ }
357
+ catch ( Exception e )
358
+ {
359
+ Console . ForegroundColor = ConsoleColor . Red ;
360
+ Console . WriteLine ( e . Message ) ;
361
+ }
362
+ finally
363
+ {
364
+ Console . ForegroundColor = ConsoleWatch . _consoleColor ;
365
+ }
366
+ }
367
+ } ) ;
327
368
}
328
369
329
- private static void WaitForCtrlC ( )
370
+ private static Task WaitForCtrlC ( )
330
371
{
331
- var exitEvent = new ManualResetEvent ( false ) ;
372
+ var exitEvent = new AsyncManualResetEvent ( false ) ;
332
373
Console . CancelKeyPress += delegate ( object sender , ConsoleCancelEventArgs e )
333
374
{
334
375
e . Cancel = true ;
335
376
Console . WriteLine ( "Stop detected." ) ;
336
377
exitEvent . Set ( ) ;
337
378
} ;
338
- exitEvent . WaitOne ( ) ;
379
+ return exitEvent . WaitAsync ( ) ;
339
380
}
340
381
}
341
382
@@ -368,20 +409,23 @@ internal class ConsoleWatch
368
409
/// <summary>
369
410
/// The original console color
370
411
/// </summary>
371
- private static readonly ConsoleColor _consoleColor = Console . ForegroundColor ;
412
+ internal static readonly ConsoleColor _consoleColor = Console . ForegroundColor ;
372
413
373
414
/// <summary>
374
415
/// We need a static lock so it is shared by all.
375
416
/// </summary>
376
- private static readonly object Lock = new object ( ) ;
417
+ internal static readonly object Lock = new object ( ) ;
377
418
//private static readonly AsyncLock AsyncLock = new AsyncLock(); //TODO: use this
378
419
420
+ internal static DateTime PrevAlertTime ;
421
+ internal static string PrevAlertMessage ;
422
+
379
423
#pragma warning disable S2223 //Warning S2223 Change the visibility of 'DoingInitialSync' or make it 'const' or 'readonly'.
380
424
public static bool DoingInitialSync = false ;
381
425
#pragma warning restore S2223
382
426
383
427
private static ConcurrentDictionary < string , DateTime > BidirectionalConverterSavedFileDates = new ConcurrentDictionary < string , DateTime > ( ) ;
384
- private static readonly AsyncLockQueueDictionary FileEventLocks = new AsyncLockQueueDictionary ( ) ;
428
+ private static readonly AsyncLockQueueDictionary < string > FileEventLocks = new AsyncLockQueueDictionary < string > ( ) ;
385
429
386
430
387
431
#pragma warning disable S1118 //Warning S1118 Hide this public constructor by making it 'protected'.
@@ -422,7 +466,9 @@ public static async Task WriteException(Exception ex, Context context)
422
466
message . Append ( Environment . NewLine + ex . Message ) ;
423
467
}
424
468
425
- await AddMessage ( ConsoleColor . Red , message . ToString ( ) , context ) ;
469
+
470
+ var msg = $ "[{ context . Time . ToLocalTime ( ) : yyyy.MM.dd HH:mm:ss.ffff} ] : { context . Event ? . FullName } : { message } ";
471
+ await AddMessage ( ConsoleColor . Red , msg , context , showAlert : true ) ;
426
472
}
427
473
428
474
public static bool IsSyncPath ( string fullNameInvariant )
@@ -836,7 +882,7 @@ private static async Task OnTouchedAsync(IFileSystemEvent fse, CancellationToken
836
882
// }
837
883
//}
838
884
839
- public static async Task AddMessage ( ConsoleColor color , string message , Context context )
885
+ public static async Task AddMessage ( ConsoleColor color , string message , Context context , bool showAlert = false )
840
886
{
841
887
await Task . Run ( ( ) =>
842
888
{
@@ -846,7 +892,18 @@ await Task.Run(() =>
846
892
try
847
893
{
848
894
Console . ForegroundColor = color ;
849
- Console . WriteLine ( $ "[{ context . Time . ToLocalTime ( ) : yyyy.MM.dd HH:mm:ss.ffff} ]:{ message } ") ;
895
+ Console . WriteLine ( message ) ;
896
+
897
+ if (
898
+ showAlert
899
+ && ( PrevAlertTime != context . Time || PrevAlertMessage != message )
900
+ )
901
+ {
902
+ PrevAlertTime = context . Time ;
903
+ PrevAlertMessage = message ;
904
+
905
+ MessageBox . Show ( message , "AsyncToSyncCodeRoundtripSynchroniserMonitor" ) ;
906
+ }
850
907
}
851
908
catch ( Exception e )
852
909
{
0 commit comments