Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8102789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:20:00+00:00 2026-06-05T23:20:00+00:00

I have a Parallel foreach function that creates a new instance of a class,

  • 0

I have a Parallel foreach function that creates a new instance of a class, that manipulates a picture, and saves it to the disk…

However approximately 4 times out of 400, the picture gets saved to the disk, but without being manipulated, my theory is that when it happens, some of the propperties existing in my class is null, when they are not suppost to…

The 4 (sometimes 3) errors mostly occurs in the first 10 images of the parallel loop.

There is no error message, it justs skip some of my code, for some reason… My breakpoint doesn’t work when it is parralel, so it is hard to debug.

Any advice on how to proceed / debug / fix ?

The code as requested

    private static void GenerateIcons(Effects effect)
    {
        DirectoryInfo dir = new DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\Icons\Original\"));

        FileInfo[] ff = dir.GetFiles();

        string mappath = HttpContext.Current.Server.MapPath(@"~\Icons\");

        List<string> paths = new List<string>();

        string ids = GetAllEffectIds(effect.TinyUrlCode);

        Parallel.ForEach(ff, item =>
        {
            if (!File.Exists(mappath + @"Generated\" + ids + "-" + item.Name))
            {
                paths.Add(mappath + @"Generated\" + ids + "-" + item.Name);
                ApplyEffects f = new ApplyEffects(effect, item.Name, mappath);
                f.SaveIcon();


            }
        });
        //Zip icons!
        ZipFiles(paths, effect.TinyUrlCode, ids, effect.General.Prefix);

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:20:01+00:00Added an answer on June 5, 2026 at 11:20 pm

    You can re-write it in a more functional style, to hopefully remove the threading issues:

    private static void GenerateIcons(Effects effect)
    {
        var dir     = new DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\Icons\Original\"));
        var mappath = HttpContext.Current.Server.MapPath(@"~\Icons\");
        var ids     = GetAllEffectIds(effect.TinyUrlCode);
    
        var filesToProcess = dir
            .EnumerateFiles()
            .AsParallel()
            .Select(f => new { info = f, generated = File.Exists(mappath + @"Generated\" + ids + "-" + f.Name) })
            .ToList();
    
        Parallel.ForEach(filesToProcess.Where(f => !f.generated), file =>
        {
            new ApplyEffects(effect, file.info.Name, mappath).SaveIcon();
        });
    
        //Zip icons!
        ZipFiles(filesToProcess.Select(f => f.info), effect.TinyUrlCode, ids, effect.General.Prefix);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Parallel.Foreach loop that has about 1000 rows in the DataTable,
I have the following code: Parallel.ForEach(this.listView2.CheckedItems, new ParallelOptions { MaxDegreeOfParallelism = 4 }, (CheckedItem)
I'm trying to write dynamic method that calls Parallel.ForEach. I have checked a IL
I have a multi-threaded application that calls a number of URL's from Parallel.ForEach loop.
I have an IronPython script that uses the TPL and Parallel.ForEach to process files
I have a Parallel.ForEach loop running an intensive operation inside the body. The operation
I have code that sends web requests through two parallel for each loops. Will
I have a parallel I/O project for parallel programming class, and I have to
I have 30+ tasks that can be executed in parallel. I use ThreadPool for
I have a Parallel.ForEach() async loop with which I download some webpages. My bandwidth

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.