mirror of
https://github.com/farcasclaudiu/SmartThreadPool.git
synced 2026-06-22 07:01:18 +03:00
4d6ffb5851
SmartThreadPool v2.0
46 lines
964 B
C#
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
|
|
}
|
|
}
|
|
}
|
|
}
|