Itβs a little difficult for me to understand the real behavior in this scenario. What actually happens in order not to complete the task when it is expected, but later, when SemaphoreSlim has been disposed of? Throws the following exception- System.ObjectDisposedException {"The semaphore has been disposed."}
I have a class library like -
public class ParallelProcessor { private Action[] actions; private int maxConcurrency; public ParallelProcessor(Action[] actionList, int maxConcurrency) { this.actions = actionList; this.maxConcurrency = maxConcurrency; } public void RunAllActions() { if (Utility.IsNullOrEmpty<Action>(actions)) throw new Exception("No Action Found!"); using (SemaphoreSlim concurrencySemaphore = new SemaphoreSlim(maxConcurrency)) { foreach (Action action in actions) { Task.Factory.StartNew(() => { concurrencySemaphore.Wait(); try { action(); } finally { concurrencySemaphore.Release(); } }); } } } }
And I use it like-
class Program { static void Main(string[] args) { int maxConcurrency = 3; Action[] actions = new Action[] { () => Console.WriteLine(1), () => Console.WriteLine(2), () => Console.WriteLine(3) };
Can anyone shed some light on this? Thanks in advance.
source share