I would like to do this with Parallel.ForEach. how can I do?
partial class Form1: Form
{
private List<my_class> lstmy_class = new List<my_class>();
private void execute_tpl(object source, ElapsedEventArgs e)
{
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
var tab_task = new Task[lstmy_class.Count];
try
{
for (int i = 0; i < lstmy_class.Count; i++)
{
tab_task[i] = Task.Factory.StartNew(() => lstmy_class[i].Calcul(token));
}
Task.WaitAll(tab_task);
}
catch (Exception ex)
{
Console.WriteLine("Error : "+ex.GetType());
}
}
}
class my_class{
...
public void Calcul(CancellationToken token)
{
// do work
}
}
I try with :
Parallel.ForEach<my_class>(lstChartClass, () => Calcul(token));
but the compiler don't want this syntax. I don't find the good syntax.
I try also with Parallel.ForEach<my_class>(lstChartClass, (i) => lstChartClass[i]Calcul()); but nothing works.