Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Parallel Foreach repeat approach

Tags:

c#

So basically, what I'm trying do is I want the parallel.foreach to repeat the log if an error is caught to the specific thread.

Parallel.ForEach(concurrentLogs, parallelOptions, log =>
  try{
   //Do work
   Console.WriteLine(log);
  }
  catch(Exception ex) {
  concurrentLogs.Enqueue(log); //repeat this log
  }
});

Because when I'm debugging it, if a thread catches an error (ex. io exception) it won't repeat the same log anymore.

What could be a possible approach to this?

like image 667
Anna P. Avatar asked Sep 26 '26 04:09

Anna P.


2 Answers

You are enumerating a ConcurrentQueue, which will be a snapshot of the collection and not reflect your later Enqueue items.

A quick solution would be to simply retry within the "foreach":

Parallel.ForEach(concurrentLogs, parallelOptions, log =>
{
    void DoWork(string log)
    {
        //Do work
        Console.WriteLine(log);
    }

    try
    {
        DoWork(log);
    }
    catch(Exception ex) { // or loop and keep count
        DoWork(log);
    }
});

aside: as noted in the comments, this isn't how best to handle retrying, you need to decide a strategy. Polly is great for this sort of thing.

However this suggests you don't want a ConcurrentQueue, or you aren't using ineffectively. You might want to look at a BlockingCollection, Channel, or ActionBlock (TPL Dataflow).

like image 98
Stuart Avatar answered Sep 27 '26 16:09

Stuart


            Parallel.ForEach(concurrentLogs, parallelOptions, log => { 
            bool sucess = true;
            do
            {
                try
                {
                    //Do work
                    Console.WriteLine(log);
                }
                catch (Exception ex)
                {
                    sucess = false;
                }
            }while(!sucess)

        });
like image 31
Amadeu Antunes Avatar answered Sep 27 '26 16:09

Amadeu Antunes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!