Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Updated Awaiter.WaitUntil extension method with indefinite option #181

Merged
merged 2 commits into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ protected BaseEventSystem(BaseMixedRealityProfile profile)
#region IMixedRealityEventSystem Implementation

private static int eventExecutionDepth = 0;
private readonly WaitUntil doneExecutingEvents = new WaitUntil(() => eventExecutionDepth == 0);

/// <inheritdoc />
public List<GameObject> EventListeners { get; } = new List<GameObject>();
Expand All @@ -53,7 +52,7 @@ public virtual async void Register(GameObject listener)

if (eventExecutionDepth > 0)
{
await doneExecutingEvents;
await eventExecutionDepth.WaitUntil(depth => depth == 0);
}

EventListeners.Add(listener);
Expand All @@ -66,7 +65,7 @@ public virtual async void Unregister(GameObject listener)

if (eventExecutionDepth > 0)
{
await doneExecutingEvents;
await eventExecutionDepth.WaitUntil(depth => depth == 0);
}

EventListeners.Remove(listener);
Expand Down
72 changes: 50 additions & 22 deletions XRTK-Core/Packages/com.xrtk.core/Utilities/Async/Awaiters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,46 +48,74 @@ public static class Awaiters
/// Author: Oguzhan Soykan<para/>
/// Source: https://stackoverflow.com/questions/29089417/c-sharp-wait-until-condition-is-true
/// </summary>
/// <remarks>Passing in -1 will make this wait indefinitely for the condition to be met.</remarks>
/// <typeparam name="T"></typeparam>
/// <param name="element"></param>
/// <param name="predicate">The predicate condition to meet.</param>
/// <param name="timeout">The number of seconds before timing out and throwing an exception.</param>
/// <param name="timeout">The number of seconds before timing out and throwing an exception. (-1 is indefinite)</param>
/// ReSharper disable once ExceptionNotThrown
/// <exception cref="TimeoutException">A <see cref="TimeoutException"/> can be thrown when the condition isn't satisfied after timeout.</exception>
public static async Task WaitUntil<T>(this T element, Func<T, bool> predicate, int timeout = 10)
{
using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(timeout)))
if (timeout == -1)
{
var tcs = new TaskCompletionSource<object>();

void Exception()
await WaitUntil_Indefinite(element, predicate);
}
else
{
using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(timeout)))
{
tcs.TrySetException(new TimeoutException());
tcs.TrySetCanceled();
}
var tcs = new TaskCompletionSource<object>();

cancellationTokenSource.Token.Register(Exception);
void Exception()
{
tcs.TrySetException(new TimeoutException());
tcs.TrySetCanceled();
}

while (!cancellationTokenSource.IsCancellationRequested)
{
try
cancellationTokenSource.Token.Register(Exception);

while (!cancellationTokenSource.IsCancellationRequested)
{
if (!predicate(element))
try
{
await Task.Delay(1, cancellationTokenSource.Token);
continue;
if (!predicate(element))
{
await Task.Delay(1, cancellationTokenSource.Token);
continue;
}
}
}
catch (Exception e)
{
tcs.TrySetException(e);
catch (Exception e)
{
tcs.TrySetException(e);
}

tcs.TrySetResult(Task.CompletedTask);
break;
}

tcs.TrySetResult(Task.CompletedTask);
break;
await tcs.Task;
}
}
}

private static async Task WaitUntil_Indefinite<T>(T element, Func<T, bool> predicate)
{
var tcs = new TaskCompletionSource<object>();

await tcs.Task;
while (true)
{
if (!predicate(element))
{
await Task.Delay(1);
continue;
}

tcs.TrySetResult(Task.CompletedTask);
break;
}

await tcs.Task;
}
}
}