0

I am trying to update my progressbar in Parallel.ForEach, but without any success. The progressbar always gets around to 80-90 and freezes there together with the whole process. What'm I doing wrong? Can the problem be caused because the UI thread is busy with the progressbar? I would appriciate any suggestions you give me.

Here is my code:

 Parallel.ForEach(test_Words, entry =>
        {
           Form1.fr.Invoke((Action)delegate { Form1.fr.progressBar1.Value++; });
           switch (test_Type)
           { 
               case "Definitions":
                   bagOfExercises.Add(Read(Definitions.get(entry.Value, entry.Key)));
                   break;
               case "Examples":
                   bagOfExercises.Add(Read(Examples.get(entry.Value, entry.Key)).Replace(entry.Key, new string('_', entry.Key.Length)));
                   break;
           }
        });
Lyubo Ivailov
  • 11
  • 2
  • 5
  • What class is `bagOfExercises`? What is `Read`? – mlibby Mar 03 '18 at 22:24
  • @mlibby bagOfExercises's type is ConcurrentBag and Read is a method which I just used to filter my string – Lyubo Ivailov Mar 03 '18 at 22:26
  • Does it work if you remove the switch and only leave the progressbar code? – CodingYoshi Mar 03 '18 at 22:35
  • @CodingYoshi Nope, it still freezes my program, but when I remove the progressbar iteration it works perfectly. – Lyubo Ivailov Mar 03 '18 at 22:39
  • Do you know if you're exceeding the maximum `Value` for the `ProgressBar`? That can lead to an [`ArgumentException`](https://msdn.microsoft.com/en-us/library/system.windows.forms.progressbar.value(v=vs.110).aspx#Anchor_1). – Poosh Mar 03 '18 at 22:42
  • You are accessing the progress bar from multiple threads, it is bound to deadlock somewhere along. You need to use mutex lock, see here https://stackoverflow.com/questions/14500721/how-to-do-proper-parallel-foreach-locking-and-progress-reporting – sramalingam24 Mar 03 '18 at 22:45
  • @Poosh At first I really thought that was the problem, but it turned out it wasnt, because I added an if statement to check wheter the maximum value was exceeded. – Lyubo Ivailov Mar 03 '18 at 22:48
  • @sramalingam24 I am confused and I am not sure how can I do that. Could you write a detailed answer? – Lyubo Ivailov Mar 03 '18 at 22:53
  • 2
    Try use the the [Progress class](https://msdn.microsoft.com/en-us/library/hh193692(v=vs.110).aspx) by wrapping your progress bar update in it. The class should take care of the proper synchronization and dispatching in an efficient manner. – ckuri Mar 04 '18 at 00:26
  • @ckuri I am not sure that's going to work – Lyubo Ivailov Mar 04 '18 at 17:47
  • @ckuri can you think of another potential fix? – Lyubo Ivailov Mar 05 '18 at 10:13
  • Try Interlocked.Increment(ref Form1.fr.progressBar1.Value); – sramalingam24 Mar 06 '18 at 03:59
  • I know this question is really old, but, i had the same issue today. I solved it with the removal of `useAsync = true` in the `FileStream` i used to read the files. - My problem was that the stream stopped reading at a random point sometimes at 80% sometimes on 99,89 %, now everything works like a charm. – k1ll3r8e Feb 24 '20 at 15:52

0 Answers0