本文整理汇总了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;
}
}
示例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();
}