I had an issue where I were calling the Parallel.ForEach
from the UI thread which resulted in the UI freezing, and the first thread running very slow and not keeping up with the rest.
I read on here: Why does this Parallel.ForEach code freeze the program up? that this is a bad thing to do, and then I found the code Task.Factory.StartNew(() =>
to wrap around the Parallel.ForEach
command in order to run it from a background worker instead.
The issue is that I keep getting an exception for the Parallel.ForEach
code, but with no information other than One or more errors occurred
from e.Message
.
What am I doing wrong?
Task.Factory.StartNew(() =>
{
try
{
Parallel.ForEach<ListViewItem>(filesListView.Items.Cast<ListViewItem>(), new ParallelOptions() { MaxDegreeOfParallelism = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.75) * 1.0)) }, (item, state) =>
{
Thread.Sleep(100);
if (CallToStop == true)
{
state.Break();
}
internalProcessStart(item);
});
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
});