execute Task.When all wait and then go back run

0

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)
    
asked by anonymous 20.10.2016 / 22:10

1 answer

3

If your goal is to wait for ChamaTarefas() to complete and then wait for another 10s, add Wait() to your call on Main() :

private static void Main(string[] args)
{
    while(true)
    {
        // Chamar wait faz a thread bloquear até a tarefa ser completada.
        ChamaTarefas().Wait(); 
        System.Threading.Thread.Sleep(10000);
    }
}
    
21.10.2016 / 18:58