Files
UnrealEngineUWP/Engine/Source/Programs/Shared/EpicGames.Core/AsyncUtils.cs
Ben Marsh cbe83e599d Merging additional changes from Horde fork of EpicGames.Core.
#rb none
#rnx

[CL 14967570 by Ben Marsh in ue5-main branch]
2020-12-28 14:45:25 -04:00

67 lines
1.5 KiB
C#

// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace EpicGames.Core
{
/// <summary>
/// Utility functions for manipulating async tasks
/// </summary>
public static class AsyncUtils
{
/// <summary>
/// Removes all the complete tasks from a list, allowing each to throw exceptions as necessary
/// </summary>
/// <param name="Tasks">List of tasks to remove tasks from</param>
public static void RemoveCompleteTasks(this List<Task> Tasks)
{
int OutIdx = 0;
for (int Idx = 0; Idx < Tasks.Count; Idx++)
{
if (Tasks[Idx].IsCompleted)
{
Tasks[Idx].Wait();
}
else
{
if (Idx != OutIdx)
{
Tasks[OutIdx] = Tasks[Idx];
}
OutIdx++;
}
}
Tasks.RemoveRange(OutIdx, Tasks.Count - OutIdx);
}
/// <summary>
/// Removes all the complete tasks from a list, allowing each to throw exceptions as necessary
/// </summary>
/// <param name="Tasks">List of tasks to remove tasks from</param>
/// <returns>Return values from the completed tasks</returns>
public static List<T> RemoveCompleteTasks<T>(this List<Task<T>> Tasks)
{
List<T> Results = new List<T>();
int OutIdx = 0;
for (int Idx = 0; Idx < Tasks.Count; Idx++)
{
if (Tasks[Idx].IsCompleted)
{
Results.Add(Tasks[Idx].Result);
}
else if (Idx != OutIdx)
{
Tasks[OutIdx++] = Tasks[Idx];
}
}
Tasks.RemoveRange(OutIdx, Tasks.Count - OutIdx);
return Results;
}
}
}