I want to add 1000+ tasks using Parallel.ForEach
. The code below is for sending email notifications. The problem is that it works only for around 150 ~ 200 notifications & I receive the emails, but after that the code gets frozen & there is no email received.
Can someone please guide me in the right direction.
var exceptions = new ConcurrentQueue<Exception>();
try
{
List<ParallelWorker_EmailNotification> workers = new List<ParallelWorker_EmailNotification>();
foreach (Email mail in listEmails)
{
workers.Add(new ParallelWorker_EmailNotification(mail));
}
Parallel.ForEach(workers, async worker =>
{
try
{
await worker.SendNotification();
}
catch (Exception ex)
{
exceptions.Enqueue(ex);
}
});
}
catch (Exception ex)
{
exceptions.Enqueue(ex);
}