I'm getting an ArgumentOutOfRangeException
when I'm really not sure why.
Task[] downloadTasks = new Task[music.Count];
for (int i = 0; i < music.Count; i++)
downloadTasks[i] = Task.Factory.StartNew(() => DownloadAudio(music[i], lstQueue.Items[i]));
Task.Factory.ContinueWhenAll(downloadTasks, (tasks) =>
{
MessageBox.Show("All the downloads have completed!",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
});
The error occurs when the for loop
runs when i = 1
and I'm not sure why it does this when I'm positive that music.Count = 1
.
I always tried this approach as an alternative to the for loop
and got the same exception:
int index = 0;
foreach (MusicFile song in music)
{
downloadTasks[index] = Task.Factory.StartNew(() => DownloadAudio(song, lstQueue.Items[index]));
index++;
}
Is there anything in the above code that might cause this?
I'm also not sure if this is relevant, but when I can accomplish the same thing using threads without any exception. It was only when I tried implementing tasks that this exception appeared.