예제 #1
0
 public ThreadDispatcher(Action <T> action, int limit = 0, bool highlvl = true)
 {
     _limit  = limit == 0 ? Math.Min(8, Environment.ProcessorCount / 2) : limit;
     dowork  = action ?? throw new ArgumentNullException(nameof(action));
     _events = new EventObj[_limit];
     queue   = new ConcurrentQueue <T>();
     ExecutionContext.SuppressFlow();
     for (var i = 0; i < _limit; i++)
     {
         _allRun |= (1 << i);
     }
     for (var i = 0; i < _limit; i++)
     {
         _events[i] = new EventObj()
         {
             Event = new AutoResetEvent(true), Index = i
         };
         _state |= (1 << i);
         var thread = CreateThread(i, highlvl);
         _events[i].Thread = thread;
         thread.Start(_events[i]);
     }
     ExecutionContext.RestoreFlow();
 }