Esempio n. 1
0
        public static void Run()
        {
            var limit1 = LimitParallelism.ForShortRunning(5);
            var ts     = new List <Thread>();

            for (int i = 0; i < 3; ++i)
            {
                ts.Add(new Thread(() => _Threads.RunLimit(50, 101, limit1, "short running")));
            }
            foreach (var t in ts)
            {
                t.Start();
            }

            var limit2 = LimitParallelism.ForLongRunning(5);
            var ts2    = new List <Thread>();

            for (int i = 0; i < 3; ++i)
            {
                ts2.Add(new Thread(() => _Threads.RunLimit(2000, 4001, limit2, "long running")));
            }
            foreach (var t in ts2)
            {
                t.Start();
            }

            ConsoleEx.ReadLn("\nPress ENTER to dispose...\n");

            ThreadPool.QueueUserWorkItem(state => limit1.Dispose());
            ThreadPool.QueueUserWorkItem(state => limit1.Dispose());
            ThreadPool.QueueUserWorkItem(state => limit1.Dispose());

            limit1.Dispose();
            limit2.Dispose();
        }
Esempio n. 2
0
 public static void RunLimit(int minInclusive, int maxExclusive, LimitParallelism limitParalelism, string text = null)
 {
     try
     {
         limitParalelism.Enter();
         ConsoleEx.WriteLnThreaded("entered... limit count: {0} {1}", limitParalelism.Count, text);
         Thread.Sleep(new Random().Next(minInclusive, minInclusive));
     }
     finally
     {
         limitParalelism.Leave();
         ConsoleEx.WriteLnThreaded("left... limit count: {0} {1}", limitParalelism.Count, text);
     }
 }