I'm using the Polly library to monitor/restart tasks in C#. I've got a bit of "dumb" sample code to test out the restart mechanism.
I've noticed that my await Task.WhenAll(policy_list.ToArray());
doesn't always await policies all the time. Instead, this just gets passed through immediately.
What is the proper method for an "infinite" await such that Polly keeps monitoring my tasks?
using System;
using System.IO;
using System.Collections.Generic;
using Polly;
using System.Threading.Tasks;
namespace Sampler
{
class Program
{
public static async Task Main(string[] args)
{
// create and execute policies for each sampler
List<Task> policy_list = new List<Task>();
for(int i = 0; i < 2; i++)
{
var policy = Policy
.Handle<Exception>()
.RetryForeverAsync()
.ExecuteAsync(async () => await Program.TaskMethod(i.ToString()));
policy_list.Add(policy);
}
await Task.WhenAll(policy_list.ToArray());
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
public static async Task TaskMethod(string task_id)
{
Console.WriteLine("Starting Task {0}", task_id);
while (true)
{
await Task.Delay(5000);
Console.WriteLine("Hello from task {0}", task_id);
int i = 0;
int b = 32 / i;
}
}
}
}