I have a windows service with a thread that runs every 2 minutes.
while (true)
{
try
{
repNeg.willExecuteLoopWithTasks(param1, param2, param3);
Thread.Sleep(20000);
}
Inside this I have a loop with tasks:
foreach (RepModel repModelo in listaRep)
{
Task t = new Task(() => { this.coletaFunc(repModelo.EndIp, user, tipoFilial); });
t.Start();
}
But I think this implementation is wrong. I need to only run one task for every element in the list, and, when a specific task finishes, wait a minute and start again.
M8's I need to say i have 2 situations here.
1 - I can't wait all Task Finish. Because some task can take more then 2 hours to finish and another can take only 27 seconds.
2 - My List of tasks can change. Thats why i got a Thread. Every 2 minutes My thread get a list of Tasks to execute and then start a loop.
But sometimes my Task not Finished yet and another Thread Start Again and then strange things show in my log. I tryed to use a Dictionry to solve my problem but after some time of execution, sometimes takes days, my log show:
"System.IndexOutOfRangeException"
Thread.Sleep(20000)will sleep for 20 seconds, not 2 minutes.