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?
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).
Parallel.ForEach(concurrentLogs, parallelOptions, log => {
bool sucess = true;
do
{
try
{
//Do work
Console.WriteLine(log);
}
catch (Exception ex)
{
sucess = false;
}
}while(!sucess)
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With