Skip to content

Random error while using Parallel.ForEach #18

Description

@bluedoom

System.AggregateException: One or more errors occurred. (An item with the same key has already been added. Key: 2350882391344)
---> System.ArgumentException: An item with the same key has already been added. Key: 2350882391344
at System.Collections.Generic.Dictionary2.TryInsert(TKey key, TValue value, InsertionBehavior behavior) at System.Collections.Generic.Dictionary2.Add(TKey key, TValue value)
at FreeImageAPI.FreeImage.OpenMultiBitmapFromStream(Stream stream, FREE_IMAGE_FORMAT& format, FREE_IMAGE_LOAD_FLAGS flags)
at FreeImageAPI.FreeImageBitmap.LoadFromStream(Stream stream, FREE_IMAGE_FORMAT format, FREE_IMAGE_LOAD_FLAGS flags)
at FreeImageAPI.FreeImageBitmap..ctor(String filename, FREE_IMAGE_FORMAT format, FREE_IMAGE_LOAD_FLAGS flags)
at FreeImageAPI.FreeImageBitmap..ctor(String filename, FREE_IMAGE_LOAD_FLAGS flags)
at FreeImageAPI.FreeImageBitmap..ctor(String filename)
at FreeImageAPI.FreeImageBitmap.FromFile(String filename)

Parallel.ForEach(Directory.GetFiles(dir, "*.png", SearchOption.TopDirectoryOnly), (path) =>
{
    using (var bitmap = FreeImageAPI.FreeImageBitmap.FromFile(path))
    {
        //readonly operation
    }
});

framework:
.net 6.0
both macOS and windows will get the error, but not frequently.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions