Hi, I found out what was causing my problems.

I replaced the outer List<T> with a ConcurentBag<T> and everything is
working fine.

3 Little Tips.

 1. Don't use IEnumerable and Parallel.ForEach, List<T> is much faster.
 2. Use ConcurrentBag<T> for your results, or one of the other objects in
the concurent namespace.
 3. Parallel.Foreach does wait until all processing happens, unless it hits
an exception that doesn't get passed back.

I was seeing messages "100 items returned", with 0 results in the grid.
NullReferenceExceptions in the linq queries and "99 items returned" with
100 in the grid.
WAG, I guess it was a concurrency Issue that wasn't throwing an exception
back at me.

 Davy,

The US Congress voted Pizza sauce a vegetable. Don't even try to convince
me of anything in the states is sane any more!


On Mon, Jun 17, 2013 at 4:02 PM, Mark Hurd <markeh...@gmail.com> wrote:

> I don't know enough about Parallel.ForEach to debug this, although I
> do think you existing code would need a thead-safe .Add at least.
>
> However, could you change this to:
>
> ICollection<Customer> data = GetCustomers();
>  var customers = data.AsParallel().select(
>  (source) =>
> {
>     DisplayableCustomer destination = new DisplayableCustomer();
>    Mapper.Map(source, destination);
>    return destination;
> });
>
> ? (As a .NET 3.5 developer guessing....)
>
> --
> Regards,
> Mark Hurd, B.Sc.(Ma.)(Hons.)
>
> On 17 June 2013 22:15, David Rhys Jones <djones...@gmail.com> wrote:
> > Oops sent too quickly.
> >
> >  ICollection<Customer> data = GetCustomers();
> >  Parallel.ForEach(
> >  data,
> >  (source) =>
> > {
> >     DisplayableCustomer destination = new DisplayableCustomer();
> >    Mapper.Map(source, destination);
> >    customers.Add(destination);
> > });
> >
> >  later on I order the data.
> >
> >  var sorted = from c in customers orderby c.ZipCode descending select c;
> >
> >  the error, occurs ( very rarely)  in the   orderby c.ZipCode
> > NullReferenceException :  c is null.
> >
> > What am I doing wrong?  the parallel should all complete when it finishes
> > right, it's not waiting for thread pools to be created / deleted and
> > returning before the data is done?
> >
> > the ICollection is a List<Customer> but I could change that to a
> threadsafe
> > list if needed.
> >
> > Davy,
> >
> > The US Congress voted Pizza sauce a vegetable. Don't even try to
> convince me
> > of anything in the states is sane any more!
> >
>

Reply via email to