Ronald Abellano

Ronald Abellano

  • NA
  • 161
  • 37.1k

Why does counting is not consistent in Parallel.For?

Dec 23 2018 3:07 AM
  1.   var items = Enumerable.Range(0, 20);  
  2.   int cnt = 0;  
  3.        
  4.             Parallel.For(0, items.Count(), (int i) => {  
  5.                 WorkOnItem(items.ToArray()[i]);  
  6.                 cnt++;  
  7.             });  
  8.   
  9. static void WorkOnItem(object item)  
  10.         {  
  11.             Console.WriteLine("Working on item:" + item);  
  12.             Thread.Sleep(10);  
  13.         }   
     
     
     
     
    Does it mean the thread failed?

Answers (1)