예제 #1
0
        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);
        }
예제 #2
0
        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);
                }
            }
        }