Пример #1
0
        public void TestQueueStrategy_Never()
        {
            for (int i = 0; i < 5; i++)
            {
                _pool.QueueUserWorkItem(Print, "i'm item " + i);
            }

            Assert.IsTrue(_pool.QueueCount > _info.MaxQueueCount);
        }
Пример #2
0
        public void TestShortTerm_MinThreadNeverExit()
        {
            for (int i = 0; i < 3; i++)
            {
                _pool.QueueUserWorkItem(Print, "i'm item " + i);
                Thread.Sleep(1000);
            }

            Assert.AreEqual(_pool.ThreadCount, 1);
        }
Пример #3
0
 public void TestLongTerm_Use4Threads()
 {
     for (int i = 0; i < 7; i++)
     {
         _pool.QueueUserWorkItem(Print, "i'm item " + i);
     }
     //_pool.WaitAll();
     Thread.Sleep(7000);
     Assert.IsTrue(_pool.MaxThreadCount == 4);
 }
Пример #4
0
        public void Test_WaitSingle()
        {
            List <int> list = new List <int>();

            for (int i = 0; i <= 10000; i++)
            {
                list.Add(i);
            }
            IWorkItem item = _pool.QueueUserWorkItem(Avg, list);

            Assert.AreEqual(5000, item.GetResult().Result);
        }
Пример #5
0
        private static void TestMyThreadPool()
        {
            StartInfo info = new StartInfo {
                Timeout = 1, MinWorkerThreads = 1
            };
            IThreadPool pool = ThreadPoolFactory.Create(info, "long term pool");

            for (int i = 0; i < 5; i++)
            {
                pool.QueueUserWorkItem(Print, "i'm item" + i, "item" + i);
            }
            pool.WaitAll();
        }
Пример #6
0
        /// <summary>
        /// Enqueues the task with the specified user state.
        /// </summary>
        /// <param name="userState">The user supplied state.</param>
        public void Enqueue(object userState)
        {
            _threadPool.QueueUserWorkItem(
                state => {
                try
                {
                    if (_context.IsCancelled)
                    {
                        Completed(
                            this,
                            new BackgroundTaskCompletedEventArgs(null, userState, _context.IsCancelled, null)
                            );
                    }
                    else
                    {
                        _context.Initialize(userState);

                        Starting(this, EventArgs.Empty);

                        _isBusy    = true;
                        var result = _theDelegate();
                        _isBusy    = false;

                        Completed(
                            this,
                            new BackgroundTaskCompletedEventArgs(result, userState, _context.IsCancelled, null)
                            );
                    }
                }
                catch (Exception e)
                {
                    _isBusy = false;

                    Completed(
                        this,
                        new BackgroundTaskCompletedEventArgs(null, userState, _context.IsCancelled, e)
                        );
                }
                finally
                {
                    _context.Teardown();
                }
            }, userState);
        }
 /// <summary>
 /// Queues a method for the execution, and specifies an object to be used by the method.
 /// </summary>
 /// <param name="threadPool">An instance of the <see cref="IThreadPool"/>.</param>
 /// <param name="work">A <see cref="WaitCallback"/> representing the method to execute.</param>
 public static void QueueUserWorkItem(this IThreadPool threadPool, WaitCallback work)
 {
     threadPool.QueueUserWorkItem(work, null);
 }
Пример #8
0
 protected override void Start(object userState)
 {
     _threadPool.QueueUserWorkItem(state => DoWork(userState), userState);
 }