0

I have a program that get user int input "1" and increment it based on the amount of files in a directory then stamp that int on each file( first is 1 and so on 1++). The foreach loop go in each directory gets its files, increment the input and call the stamp method until all files are done. In this process the order is important. However multitasking ( Parallel.ForEach) does't always guarantee order, in my understanding it returns which ever thread done first and maybe also damage the i++ functionality ( correct me if I'm wrong).

The question is how to apply multi threading in this case? i am thinking save the values of the foreach at the end, pass it to the stamping method and have the method stamp x amount of files at a time. I don't know if its possible or how to apply.

Here is my watermark method:

//text comes from the foreach already set.
  public void waterMark(string text, string sourcePath, string destinationPathh)
        {
            using (Bitmap bitmap = new Bitmap(sourcePath))
            {
                //somecode
                using (Graphics graphics = Graphics.FromImage(tempBitmap))
                {
                   //somecode
                   tempBitmap.Save(destinationPathh, ImageFormat.Tiff);
                  //Erroe^: a generic error occurred in gdi+
                 //I think due to trying to save multiple files at once
                }
            }
        }

The foreach loop:

var files = folder.GetFiles();
 Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 4 }, (file, state,indexer) =>
                {
//somecode that calls the waterMark method in multiple spots as of now
});

Thank you in advance.

CountLessQ
  • 181
  • 1
  • 11
  • 1
    See also: [Read and process files in parallel C#](https://stackoverflow.com/questions/20928705/read-and-process-files-in-parallel-c-sharp). There is [Parallel.For Method](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel.for?view=netframework-4.8) method giving you an index. – Olivier Jacot-Descombes Jan 21 '20 at 16:55

1 Answers1

2

There is an overload of Parallel.ForEach that also provides an index for the item being processed:

Parallel.ForEach(someEnumerable, (val, state, idx) => Console.WriteLine(idx))

You can use it to keep track of the index in a thread-safe fashion.

As for the GDI+ stuff (Bitmap), I think you're safe as long as you use a single thread for all interactions with the bitmap. Don't try to do anything clever with async between instantiation and disposal.

spender
  • 117,338
  • 33
  • 229
  • 351
  • @CountLessQ No... I'm not going to provide support through your development process. You've encountered a clear error message. Time to write a more focused question concerning that error message. Chameleon questions are generally of little use to subsequent readers. – spender Jan 21 '20 at 17:42
  • @CountLessQ Feel free to ping me when you've written the question... I don't know how useful I'll be though. I never had much luck using GDI+ for bulk image operations. – spender Jan 21 '20 at 17:43
  • I think same issue/question still apply. It is the usage of multi threading while manipulating the files as mentioned, thank you for your help. – CountLessQ Jan 21 '20 at 17:51