コード例 #1
0
        public bool SetMinThreads(uint workerThreads)
        {
            CheckForDisposed();
            if (workerThreads == 0)
            {
                return(false);
            }
            if (_minThread == workerThreads)
            {
                return(false);
            }

            lock (_lockObject)
            {
                if (_minThread < workerThreads)
                {
                    for (uint i = 0; i < workerThreads - _minThread; i++)
                    {
                        threadList.Add(CreateThread());
                    }
                }
                else
                {
                    for (uint i = 0; i < _minThread - workerThreads; i++)
                    {
                        ThreadContainer tc = threadList[0];
                        threadList.RemoveAt(0);
                        tc.MarkForDispose();
                    }
                }
                _minThread = workerThreads;
            }
            return(true);
        }
コード例 #2
0
        private ThreadContainer CreateThread()
        {
            var tc = new ThreadContainer(new Thread(WorkerTask)
            {
                IsBackground = true, Name = UniqueName + "_WorkerThread_" + Guid.NewGuid().ToString()
            });

            tc.Thread.Start(tc);

            return(tc);
        }
コード例 #3
0
        //public bool UnsafeQueueUserWorkItem(WaitCallback callBack, object state)
        //{
        //    return this.UnsafeQueueUserWorkItem(defaultGroupName, callBack, state);
        //}
        //public bool UnsafeQueueUserWorkItem(string groupName, WaitCallback callBack, object state)
        //{
        //    throw new NotImplementedException();
        //}

        private void WorkerTask(object state)
        {
            ThreadContainer tc = state as ThreadContainer;

            while (!tc.ShouldDispose)
            {
                if (_current == null)
                {
                    manualReset.Wait();
                    lock (taskGroups)
                    {
                        if (taskGroups.First == null)
                        {
                            manualReset.Reset();
                            continue;
                        }
                        if (_current == null)
                        {
                            _current = taskGroups.First;
                        }
                    }
                    continue;
                }
                LinkedListNode <TaskGroup> item = null;
                lock (taskGroups)
                {
                    if (_current == null)
                    {
                        continue;
                    }
                    item     = _current;
                    _current = _current.Next;
                }

                Interlocked.Increment(ref _threadInUseCounter);

                //foreach (TaskGroup item in taskGroups.GetConsumingEnumerable())
                try
                {
                    if (item.Value.Dequeue(out InnerTask innerTask))
                    {
                        try
                        {
                            innerTask.WaitCallBack(innerTask.State);
                        }
                        catch
                        {
                            //TODO: Handle Exception
                        }
                    }
                    if (item.Value.Count == 0)
                    {
                        lock (item)
                        {
                            if (item.Value.Count == 0 && item.Value.IsQueued)
                            {
                                item.Value.IsQueued = false;
                                lock (taskGroups)
                                {
                                    taskGroups.Remove(item);
                                }
                            }
                        }
                    }
                }
                finally{
                    Interlocked.Decrement(ref _threadInUseCounter);
                }
            }
        }