0
I’m trying to make a console program, run a task, wait and run again and continue in this cycle endlessly.
In theory it would place the function within a While(true)
infinite loop, but as the function is asynchronous 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 didn’t work, it runs the task, waits 10 seconds and is already creating another task, I tried to do something like:
var executando = ChamaTarefas();
if(executando.IsCompleted)
particularly I found very confusing the explanation of the question. What do you mean by "continue in this cycle"? I couldn’t understand exactly where your doubt is. In time: it’s not bad to use windows Scheduler. It possess much more advanced features than we can implement alone and fast. There are alternatives in . NET with advanced task scheduling features. Have a look at: http://www.quartz-scheduler.net/
– Loudenvier
in case it is like executing an instruction, wait, run again.
– Dorathoto