Parallel.ForEach questions

I am using a Parallel.ForEach loop in C# / VS2010 to do processing and I have a couple of questions.

First of all I have a process that needs to extract information from a remote webservice and then needs to build images (GDI) on the fly.

I have a class that encapsulates all of the functionality into a single object with two main methods Load() and CreateImage() with all the GDI management / WebRequests "blackboxed" inside this object.

I then create a GenericList that contains all the objects that need to be processed and I iterate through the list using the following code:

try
        {
            Parallel.ForEach(MyLGenericList, ParallelOptions, (MyObject, loopState) =>
            {                                       

                    MyObject.DoLoad();
                    MyObject.CreateImage();
                    MyObject.Dispose();

                if (loopState.ShouldExitCurrentIteration || loopState.IsExceptional)
                    loopState.Stop();
            });
        }
        catch (OperationCanceledException ex)
        {
            // Cancel here
        }
        catch (Exception ex)
        {
            throw ex;
        }

Now my questions are:

  1. Given that there could be ten thousand items in the list to parse, is the above code the best way to approach this? Any other ideas more then welcome
  2. I have an issue whereby when I start the process the objects are created / loaded and images created very fast but after around six hundred objects the process starts to crawl. It doesn eventually finish, is this normal?

Thanks in advance :) Адам

9
задан Conrad Frix 17 May 2011 в 10:23
поделиться