In my main class, I have such event
private event System.Action OnActionFinished;
And knowing that await calls GetAwaiter() method, I have System.Action extension method: `
public static TaskAwaiter<object> GetAwaiter(this Action action)
{
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
action += () => tcs.SetResult(null);
return tcs.Task.GetAwaiter();
}
in order to be able to write await OnActionFinished;
in async
method.
public async Task ExecuteTurns(Queue<TurnAction> turnActions)
{
foreach (TurnAction turn in turnActions)
{
Action<Vector2> TurnMethod = null;
switch (turn.TurnType)
{
case TurnType.Walk:
TurnMethod = Walk;
break;
case TurnType.Jump:
TurnMethod = Jump;
break;
case TurnType.Shoot:
TurnMethod = rangedWeapon.Shoot;
break;
}
Task.Run(async () => {
TurnMethod(turn.Direction);
});
await OnActionFinished;
}
}
OnActionFinished is invoked in the end of Walk, Jump, And Shoot methods.
Why does this not work?