当前位置: 首页>>代码示例>>C#>>正文


C# SmartThreadPool.Cancel方法代码示例

本文整理汇总了C#中Amib.Threading.SmartThreadPool.Cancel方法的典型用法代码示例。如果您正苦于以下问题:C# SmartThreadPool.Cancel方法的具体用法?C# SmartThreadPool.Cancel怎么用?C# SmartThreadPool.Cancel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Amib.Threading.SmartThreadPool的用法示例。


在下文中一共展示了SmartThreadPool.Cancel方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Start

        /// <summary>
        /// Starts the tasks execution.
        /// </summary>
        /// <returns>If has reach the timeout false, otherwise true.</returns>
        public override bool Start()
        {
            base.Start();
            m_threadPool = new SmartThreadPool();

            try
            {
                m_threadPool.MinThreads = MinThreads;
                m_threadPool.MaxThreads = MaxThreads;
                var workItemResults = new IWorkItemResult[Tasks.Count];

                for (int i = 0; i < Tasks.Count; i++)
                {
                    var t = Tasks[i];
                    workItemResults[i] = m_threadPool.QueueWorkItem(new WorkItemCallback(Run), t);
                }

                m_threadPool.Start();

                // Timeout was reach?
                if (!m_threadPool.WaitForIdle(Timeout.TotalMilliseconds > int.MaxValue ? int.MaxValue : Convert.ToInt32(Timeout.TotalMilliseconds)))
                {
                    if (m_threadPool.IsShuttingdown)
                    {
                        return true;
                    }
                    else
                    {
                        m_threadPool.Cancel(true);
                        return false;
                    }
                }

                foreach (var wi in workItemResults)
                {
                    Exception ex;
                    wi.GetResult(out ex);

                    if (ex != null)
                    {
                        throw ex;
                    }
                }

                return true;
            }
            finally
            {
                m_threadPool.Shutdown(true, 1000);
                m_threadPool.Dispose();
                IsRunning = false;
            }
        }
开发者ID:denmerc,项目名称:GeneticSharp,代码行数:57,代码来源:SmartThreadPoolTaskExecutor.cs

示例2: CancelSTPWorkItems

        public void CancelSTPWorkItems()
        {
            // I don't use lock on the counter, since any number above 0 is a failure.
            // In the worst case counter will be equal to 1 which is still not 0.
            int counter = 0;

            SmartThreadPool stp = new SmartThreadPool();

            for (int i = 0; i < 10; i++)
            {
                stp.QueueWorkItem(
                    state =>  { Thread.Sleep(500); ++counter; return null; }
                    );
            }

            Thread.Sleep(100);

            stp.Cancel(true);

            Assert.AreEqual(counter, 0);

            stp.Shutdown();
        }
开发者ID:amibar,项目名称:SmartThreadPool,代码行数:23,代码来源:TestCancel.cs


注:本文中的Amib.Threading.SmartThreadPool.Cancel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。