Пример #1
0
        private void btnAbortableThreadPool_Click(object sender, EventArgs e)
        {
            Console.WriteLine(string.Format("QueueCount:{0},WorkingCount:{1}", AbortableThreadPool.QueueCount, AbortableThreadPool.WorkingCount));

            WorkItem workItem1 = AbortableThreadPool.QueueUserWorkItem(new WaitCallback(Test));

            Console.WriteLine(string.Format("QueueCount:{0},WorkingCount:{1}", AbortableThreadPool.QueueCount, AbortableThreadPool.WorkingCount));

            WorkItem workItem2 = AbortableThreadPool.QueueUserWorkItem(new WaitCallback(Test));
            WorkItem workItem3 = AbortableThreadPool.QueueUserWorkItem(new WaitCallback(Test));
            WorkItem workItem4 = AbortableThreadPool.QueueUserWorkItem(new WaitCallback(Test));
            WorkItem workItem5 = AbortableThreadPool.QueueUserWorkItem(new WaitCallback(Test));

            Console.WriteLine(string.Format("QueueCount:{0},WorkingCount:{1}", AbortableThreadPool.QueueCount, AbortableThreadPool.WorkingCount));
            Thread.Sleep(1000);

            Console.WriteLine(AbortableThreadPool.Cancel(workItem1, false));
            Console.WriteLine(string.Format("QueueCount:{0},WorkingCount:{1}", AbortableThreadPool.QueueCount, AbortableThreadPool.WorkingCount));
            Thread.Sleep(1000);

            Console.WriteLine(AbortableThreadPool.Cancel(workItem1, true));
            Console.WriteLine(string.Format("QueueCount:{0},WorkingCount:{1}", AbortableThreadPool.QueueCount, AbortableThreadPool.WorkingCount));
            Thread.Sleep(1000);

            //AbortableThreadPool.CancelAll(true);//可取消所有任务
            AbortableThreadPool.Join(); //等待所有任务退出
            Console.WriteLine(string.Format("QueueCount:{0},WorkingCount:{1}", AbortableThreadPool.QueueCount, AbortableThreadPool.WorkingCount));
        }
Пример #2
0
        internal void SetIsExpanded(TreeNodeAdv node, bool value, bool ignoreChildren)
        {
            ExpandArgs eargs = new ExpandArgs();

            eargs.Node           = node;
            eargs.Value          = value;
            eargs.IgnoreChildren = ignoreChildren;

            if (AsyncExpanding && LoadOnDemand && !_threadPool.IsMyThread(Thread.CurrentThread))
            {
                WaitCallback wc = delegate(object argument) { SetIsExpanded((ExpandArgs)argument); };
                _threadPool.QueueUserWorkItem(wc, eargs);
            }
            else
            {
                SetIsExpanded(eargs);
            }
        }