private bool TryImmediately(MooBackendTask task) { bool canRun = false; lock (_syncRoot) { if (_disposed) { throw new ObjectDisposedException("MooThreadPool"); } canRun = _runners.Any(x => x.IsInsideRunnerThread()); } if (canRun) { try { task.Action(); } catch (Exception error) { Environment.FailFast("Unhandled exception while triggering backend task", error); } return(true); } return(false); }
private void Add(MooBackendTask task, int priority) { lock (_syncRoot) { if (_disposed) { throw new ObjectDisposedException("MooThreadPool"); } bool shouldExpand = _runners.Count < _maxThreads && _runners.All(x => x.IsBusy); _context.Queue.Add(task, priority); _context.TaskAddedSignal.Set(); if (shouldExpand) { var newRunner = new MooBackendRunner(_context); newRunner.Start(); _runners.Add(newRunner); } } }