Hello,
I want to create asynchronous tasks in a foreach-loop.
The algorythm takes the first six values of the foreach-loop correctly. But than the task factory sets the rest of the asynchonous tasks on Wait-Status. The foreach-loop continues, so that when the next asyncronous tasks start, the value of the foreach-loop isn't right and I have a lot of tasks, which will take the same value.
In my opinion the right way is, to call the asynchronous task with the value of the foreach-loop. Can anyone help me, in the best with an example.
Here is the code, which I use, until now:
Thanks in forward
Kostarsus
I want to create asynchronous tasks in a foreach-loop.
The algorythm takes the first six values of the foreach-loop correctly. But than the task factory sets the rest of the asynchonous tasks on Wait-Status. The foreach-loop continues, so that when the next asyncronous tasks start, the value of the foreach-loop isn't right and I have a lot of tasks, which will take the same value.
In my opinion the right way is, to call the asynchronous task with the value of the foreach-loop. Can anyone help me, in the best with an example.
Here is the code, which I use, until now:
Code:
foreach (WFileInfo fileInfo in fileInfos)
{
mp3InfoReadertasks.Enqueue(Task.Factory.StartNew(()=>
{
try
{
log.DebugFormat("ReadMP3_Info: path={0}, filename={1}", fileInfo.Path, fileInfo.Filename);
WMP3FileInfo newInfo = mp3Reader.readFile(fileInfo);
mp3Infos.Add(newInfo);
}
catch (Exception ex)
{
errors.Add(new WSynchronizeError() { Path = fileInfo.Path, Filename = fileInfo.Filename, Message = ex.Message });
}
}));
}
Thanks in forward
Kostarsus