Bug or wrong code? RRS feed

  • Question

  • Hi,


    I've tried to use PLINQ to load and evaluate images from files. So I use Select at first to get Image from file and then Where with condition about Image. Well, maybe it's unusual, but I think it's semantically and surely syntactically correct (see bellow).

    What's strange - Select was called twice for each file (found by including Console.WriteLine in Select method). Firstly I thought that something wrong with threads but even in case of degree of paralellism = 1 (as shown in code) the result was same. Without AsParallel this code works perfectly.


    var largeImages = fileNames.AsParallel(1).Select(fName =>


    var fileImg = new { fName, img = Image.FromFile(fName)};

    Console.WriteLine("Loaded file: {0} on thread {1}", Path.GetFileName(fileImg.fName), Thread.CurrentThread.ManagedThreadId);

    return fileImg;

    }).Where(fileImg =>


    return fileImg.img.Width > Width || fileImg.img.Height > Height;


    foreach (var fileImg in largeImages)


    // do something with images




    So my question is - is it bug in AsParallel (or some lower level) implementation or is my code wrong? For the second case - could you tell me what I'm doing wrong?


    TIA. Best regards,

    Vlada B.

    Tuesday, March 25, 2008 7:05 PM


  • Hi Vlada,


    Your expectations about how the code should behave seem very reasonable. I looked into the issue, and it looks like a bug in PLINQ. It seems to be caused by the Where operator, which may cause the delegate in Select to execute twice. Ooops. We'll look into it more, and figure out how to address it.


    Thanks for reporting the problem! Your feedback is very helpful. Let us know else that you find while playing around with our CTP.



    Tuesday, March 25, 2008 11:03 PM