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 8445597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:41:45+00:00 2026-06-10T09:41:45+00:00

I have a list of files that needs to be processed at the same

  • 0

I have a list of files that needs to be processed at the same time. I tried to use ForAll extension method of the ParallelQuery class. I don’t the files to be processed in order so I used ForAll.

Here’s my sample code:

List<FileInfo> files = GetFilesToProcess();

files.AsParallel().ForAll(f => { // Process file here });

It works great but now I need to generate a unique integer ID for each of the file and I not sure how to do it without changing the AsParallel.ForAll to ForEach.

I read somewhere that I need to as an Interlocked but there will still be an issue.

Hope you can give me an idea here.

Thanks!

  • 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-10T09:41:47+00:00Added an answer on June 10, 2026 at 9:41 am

    You can use Interlocked.Increment to generate the ID, or you can just use the index directly:

    List<FileInfo> files = GetFilesToProcess();
    
    files.AsParallel().Select((f, i) => new {File=f, ID=i})
         .ForAll(fp => 
               {
                  FileInfo file = fp.File;
                  int id = fp.ID; // ID is the index in the list
    
                  // Process file here
               });
    

    If you want to use Interlocked.Increment, you could do:

    List<FileInfo> files = GetFilesToProcess();
    int globalId = -1;
    
    files.AsParallel().ForAll(f => 
                             { 
                                  // Process file here 
                                  int id = Interlocked.Increment(ref globalId);
                                  // use ID
                             });
    

    That being said, if your entire goal is to do “work” on a collection, I would recommend writing this as a Parallel.For or Parallel.ForEach instead. This is much more clear, as you’re not using LINQ style syntax for the sole purpose of generating side effects:

    List<FileInfo> files = GetFilesToProcess();
    Parallel.For(0, files.Count, i =>
    {
        var file = files[i];
        // Use i and file as needed
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of files that I would like analyze. They are all
I have a list of files that I'm trying to copy and move (using
I have a long list of files that all end in a date in
Let's imagine that I have list of files at host1 find /path/to -name *.jpg
I have a text file that contains a list of filenames, minus the extension,
I have a Java program that needs to call the same external executable 6
I have a Python script that needs to process a large number of files.
I have a large list of file names that are illegal, I want to
I have a file that contain list of numbers that looks like this: 10^-92
I have a file that essentially a list of XPaths like so: /Options/File[1]/Settings[1]/Type[1] /Options/File[1]/Settings[1]/Path[1]

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.