0

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);
    }
});
Community
  • 1
  • 1
user2924019
  • 1,983
  • 4
  • 29
  • 49
  • 2
    Well instead of printing `e.Message`, print out the complete exception - just `e.ToString()` will do it. There's probably an `AggregateException`, and the real exception will be in the `InnerExceptions` collection. – Jon Skeet Oct 02 '16 at 19:55
  • Here is the full error: http://imglnk.uk/img?i=JoHlcX – user2924019 Oct 02 '16 at 21:20
  • Please put the text of the error directly into the question. Looks pretty simple to me though - you're trying to access the UI from a non-UI thread. – Jon Skeet Oct 02 '16 at 21:21
  • The issue is that using the UI thread to initiate the `Parallel.ForEach` causes UI problems and freezes often. I read that you're not supposed to use the UI to make this call. – user2924019 Oct 02 '16 at 21:30
  • I didn't say you should call it on the UI thread. I explained what was causing the exception, although we don't know exactly where as you haven't shown what your processing code is doing, but accessing the Items property looks like a bad idea to start with. Basically, you shouldn't touch the UI in any of this code. – Jon Skeet Oct 02 '16 at 21:32
  • I suspect it is in line 5 of the code posted in the question which is causing it as it makes a reference to the fileslistview which is a listview control. I will see if putting this into an array first fixes the issue. All other calls to the UI are made with Invoke – user2924019 Oct 02 '16 at 21:34
  • I changed this to use an array instead which fixes the issue and it now runs, however it seems to be causing more issues for the UI, and some threads are getting stuck where as they didn't before when not using the background task. – user2924019 Oct 02 '16 at 21:39

1 Answers1

1

For those who have the same problem, in any kind of background thread, try not to use any UI dependent variables such as Text or ListViewItems. To use these variables, get them into a new variable before calling background task. then use this variable in task.

ListViewItem myIndependentList = filesListView.Items.Cast<ListViewItem>();
darioosh10
  • 31
  • 4