Files
SmartThreadPool/STPExamples/CooperativeCancelExample.cs
T
Ami Bar 4d6ffb5851 v2.0
SmartThreadPool v2.0
2009-12-19 16:32:41 +02:00

46 lines
964 B
C#

using System.Threading;
using Amib.Threading;
namespace Examples
{
public class CooperativeCancelExample
{
public void DoWork(object state)
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
// Queue the work item
IWorkItemResult wir = smartThreadPool.QueueWorkItem(DoRealWork);
// Give the work item some time to complete.
Thread.Sleep(1000);
// If the work item hasn't completed yet then cancel it.
if (!wir.IsCompleted)
{
wir.Cancel();
}
smartThreadPool.Shutdown();
}
// Do some lengthy work
private void DoRealWork()
{
// Do something here.
// Sample SmartThreadPool.IsWorkItemCanceled
if (SmartThreadPool.IsWorkItemCanceled)
{
return;
}
// Sample the SmartThreadPool.IsWorkItemCanceled in a loop
while (!SmartThreadPool.IsWorkItemCanceled)
{
// Do some real work here
}
}
}
}