Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hez2010 committed Sep 3, 2021
1 parent 37433fe commit 930fdb5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
17 changes: 10 additions & 7 deletions Files/Helpers/AsyncManualResetEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ public class AsyncManualResetEvent
{
private volatile TaskCompletionSource<bool> m_tcs = new TaskCompletionSource<bool>();

public Task WaitAsync(CancellationToken cancellationToken = default)
public async Task WaitAsync(CancellationToken cancellationToken = default)
{
var tcs = m_tcs;
var cancelTcs = new TaskCompletionSource<bool>();

cancellationToken.Register(
s => ((TaskCompletionSource<bool>)s).TrySetCanceled(), tcs);
s => ((TaskCompletionSource<bool>)s).TrySetCanceled(), cancelTcs);

return m_tcs.Task;
await await Task.WhenAny(tcs.Task, cancelTcs.Task);
}

private async Task<bool> Delay(int milliseconds)
Expand All @@ -26,15 +28,15 @@ private async Task<bool> Delay(int milliseconds)
return false;
}

public Task<bool> WaitAsync(int milliseconds, CancellationToken cancellationToken = default)
public async Task<bool> WaitAsync(int milliseconds, CancellationToken cancellationToken = default)
{
var tcs = m_tcs;
var cancelTcs = new TaskCompletionSource<bool>();

cancellationToken.Register(
s => ((TaskCompletionSource<bool>)s).TrySetCanceled(), tcs);

s => ((TaskCompletionSource<bool>)s).TrySetCanceled(), cancelTcs);

return Task.WhenAny(m_tcs.Task, Delay(milliseconds)).Result;
return await await Task.WhenAny(m_tcs.Task, cancelTcs.Task, Delay(milliseconds));
}

public void Set()
Expand All @@ -43,6 +45,7 @@ public void Set()
Task.Factory.StartNew(s => ((TaskCompletionSource<bool>)s).TrySetResult(true),
tcs, CancellationToken.None, TaskCreationOptions.PreferFairness, TaskScheduler.Default);
tcs.Task.Wait();

}

public void Reset()
Expand Down
6 changes: 4 additions & 2 deletions Files/ViewModels/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -909,11 +909,13 @@ public async Task LoadExtendedItemProperties(ListedItem item, uint thumbnailSize
item.ItemPropertiesInitialized = true;
itemLoadQueue[item.ItemPath] = false;

var cts = loadPropsCTS;

try
{
await Task.Run(async () =>
{
await loadPropsEvent.WaitAsync(loadPropsCTS.Token);
await loadPropsEvent.WaitAsync(cts.Token);

if (itemLoadQueue.TryGetValue(item.ItemPath, out var canceled) && canceled)
{
Expand Down Expand Up @@ -1039,7 +1041,7 @@ await FilesystemTasks.Wrap(() => CoreApplication.MainView.DispatcherQueue.Enqueu
}));
}
}
}, loadPropsCTS.Token);
}, cts.Token);
}
catch (OperationCanceledException)
{
Expand Down

0 comments on commit 930fdb5

Please sign in to comment.