I'm trying to run two methods in parallel. The first method connects to an ftp server and downloads some data. Because I want to reduce the network traffic it should run every 30 s. In parallel I want another method to run independent from the first method every 10 s.
The problem is I don't get the methods running/delayed in parallel.
namespace Example
{
class Program
{
static async Task Main(string[] args)
{
await Task.Run(async () =>
{
while (true)
{
await Every10s();
await Every30s();
}
});
}
public static async Task<bool> Every10s()
{
await Task.Delay(10000);
Console.Writeline("10s");
return true;
}
public static async Task<bool> Every30s()
{
await Task.Delay(30000);
Console.Writeline("30s");
return true;
}
}
}
I would expect the following output with the corresponding pauses in between: 10s 10s 10s 30s 10s 10s 10s 30s ...
But instead both methods wait for each other so I get the output 10s 30s 10s 30s 10s 30s with a 40s pause.
Any help and hints are appreciated.
await Task.WhenAll(list)on it. You'd need to do that on each while-cycle. Note, for this use case I'd suggest using timers over async/await since this is literally the definiton of a timer. Another reason not to use async/await here is because async/await isn't truly parallel (I'd link to an awesome write up here but I forgot where it's from).