44using System . IO ;
55using System . Linq ;
66using System . Threading ;
7+ using System . Threading . Tasks ;
78using PCL . Core . App ;
89using PCL . Core . UI ;
910using PCL . Core . Utils ;
@@ -157,7 +158,7 @@ public static void RegisterDefaultProcess(FileMatch match, FileProcess process)
157158 private static readonly ConcurrentQueue < IFileTask > _PendingTasks = [ ] ;
158159
159160 private static readonly ConcurrentDictionary < FileItem , AtomicVariable < object > > _ProcessResults = [ ] ;
160- private static readonly ConcurrentDictionary < FileItem , ManualResetEventSlim > _WaitForResultEvents = [ ] ;
161+ private static readonly ConcurrentDictionary < FileItem , AsyncManualResetEvent > _WaitForResultEvents = [ ] ;
161162 private static readonly AutoResetEvent _ContinueEvent = new ( false ) ;
162163 private static bool _running = true ;
163164
@@ -300,7 +301,7 @@ public static bool TryGetResult(FileItem item, out AnyType? result, bool remove
300301 if ( remove ) _ProcessResults . TryRemove ( item , out _ ) ;
301302 return true ;
302303 }
303-
304+
304305 /// <param name="item">which file to get the result</param>
305306 /// <param name="remove">whether remove from the temp dictionary after successfully get the value</param>
306307 /// <returns>a <b>nullable</b> value</returns>
@@ -322,7 +323,7 @@ public static bool TryGetResult(FileItem item, out AnyType? result, bool remove
322323 {
323324 var success = TryGetResult ( item , out var result , remove ) ;
324325 if ( success ) return result ;
325- var waitEvent = _WaitForResultEvents . GetOrAdd ( item , _ => new ManualResetEventSlim ( false ) ) ;
326+ var waitEvent = _WaitForResultEvents . GetOrAdd ( item , _ => new AsyncManualResetEvent ( ) ) ;
326327 var waitResult = true ;
327328 if ( timeout is { } t ) waitResult = waitEvent . Wait ( t ) ;
328329 else waitEvent . Wait ( ) ;
@@ -331,6 +332,26 @@ public static bool TryGetResult(FileItem item, out AnyType? result, bool remove
331332 return result ;
332333 }
333334
335+ /// <param name="item">which file to wait for the result</param>
336+ /// <param name="cancelToken">the cancellation token to stop waiting</param>
337+ /// <param name="remove">whether remove from the temp dictionary after successfully get the value</param>
338+ /// <returns>
339+ /// a value, or <c>null</c> if the result is really <c>null</c>, or else, something is boom -
340+ /// I don't know what is wrong but in a word there is something wrong :D
341+ /// </returns>
342+ public static async Task < AnyType ? > WaitForResultAsync ( FileItem item , CancellationToken cancelToken = default , bool remove = true )
343+ {
344+ var success = TryGetResult ( item , out var result , remove ) ;
345+ if ( success ) return result ;
346+ var waitEvent = _WaitForResultEvents . GetOrAdd ( item , _ => new AsyncManualResetEvent ( ) ) ;
347+ var waitResult = true ;
348+ cancelToken . Register ( ( ) => waitResult = false ) ;
349+ await waitEvent . WaitAsync ( cancelToken ) ;
350+ if ( ! waitResult ) return null ;
351+ TryGetResult ( item , out result ) ;
352+ return result ;
353+ }
354+
334355 #endregion
335356
336357 private static void _Initialize ( )
0 commit comments