I'm trying to develop a process that performs the collection of some printer counters via SNMP protocol, I thought about using Threads to perform this process, I refaced to use tasks, but I do not know if I should use an async await for this task, in this case I get a little confused if I'm developing this method in the best way. Here is the method code:
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
using (ImpressoraDAO dao = new ImpressoraDAO())
{
var impressoras = dao.Listar().ToList();
var impressorasTasks = impressoras.Select(impressora =>
{
return Task.Factory.StartNew(() =>
{
if (impressora.IsConected())
{
using (ContadorDAO contadorDao = new ContadorDAO())
{
var contador = new Contador();
contador.Id = DateTime.Now;
contador.ImpressoraId = impressora.Id;
contador.QuantidadePaginas = Convert.ToInt64(OperacaoSNMP.ObterObjetoOID(IPAddress.Parse(impressora.Ip), OID.CONTADADOR_TOTAL));
contadorDao.Adicionar(contador);
}
}
});
}).ToArray();
Task.WhenAll(impressorasTasks);
}
}
How can I improve this code? Should I leave it like this or would it be better to use an async await?