Hi Tom,

I've got some examples, but why do you want to do this?

       // Something to do in the loop
       Action action =
              () => Console.WriteLine(Thread.CurrentThread.ManagedThreadId);

       // standard while(true) loop
       while (true)
       {
              action();
       }

       // Now need an infinite enumerable
       private IEnumerable<bool> Forever()
       {
              return Repeat(true);
       }

       // a general purpose infinite enumerable
       private IEnumerable<T> Repeat<T>(T value)
       {
              while (true)
              {
                     yield return value;
              }
       }

       // ForEach infinite loop
       Parallel.ForEach(Forever(), b => action());
       Parallel.ForEach(Repeat(true), b => action());

       // AsParallel/ForAll infinite loop
       Forever().AsParallel().ForAll(b => action());
       Repeat(true).AsParallel().ForAll(b => action());

       // Using Interactive Extensions (Ix) Repeat extension method
       // from the Reactive Extensions (Rx) Team
       // so you don't have to roll your own
       Parallel.ForEach(EnumerableEx.Repeat(true), b => action());
       EnumerableEx.Repeat(true).AsParallel().ForAll(b => action());

Is this what you wanted?

Cheers.

James.

From: [email protected] [mailto:[email protected]] On 
Behalf Of Tom Gao
Sent: Wednesday, 26 October 2011 23:57
To: 'ozDotNet'
Subject: while (true) using Parallel.ForEach

Hi All,

Can someone please provide me with an example of how I can replicate a 
while(true){ ... } using Parallel.ForEach?

Thank you,
Tom

Reply via email to