I'm trying to do a console program, run a task, wait and run again and continue on this cycle infinitely.
In theory would be to put the function inside an infinite While(true)
loop, but as the function is asynchronous it is not working.
private static void Main(string[] args)
{
While(true)
{
ChamaTarefas();
System.Threading.Thread.Sleep(10000);
}
}
private static async Task ChamaTarefas()
{
IEnumerable<UrlsProdutos> registros = db.UrlsTable.Where(w => w.Lido == false).Take(10);
var tarefas = registros.Select((registro, index) =>
{
return Task.Run(async () => await ExecutaTarefasAsync(registro, index));
});
await Task.WhenAll(tarefas.ToArray());
}
public static async Task ExecutaTarefasAsync(UrlsProdutos registros, int index)
{
var produto = await ExtraiDados.ParseHtml(registros.Url, index);
InsertAdo.MarcaComoLidoStpAsync(registros.UrlsImoveisVivarealId, index);
await InsertAdo.InsertAdoStpAsync(produto, index);
await ManipulacaoFoto.DownloadImgAsync(produto.FotosProduto, index);
}
But of course it did not work, it runs the task, it waits 10 seconds and it already creates another task, I already tried to do something like:
var executando = ChamaTarefas();
if(executando.IsCompleted)