public static void PollForCancellation()
{
Thread t = Thread.CurrentThread;
// Don't go through the property on Thread, but access the field
// directly. We don't want to create the stack if no one in the
// thread is using cancellation.
List<CancellationSignal> signals = t.m_CancellationSignals;
if (signals != null) {
bool tookLock = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
Monitor.ReliableEnter(signals, out tookLock);
if (signals.Count > 0) {
if (signals[signals.Count - 1].CancelRequested)
throw new OperationCanceledException();
}
}
finally {
if (tookLock)
Monitor.Exit(signals);
}
}
}
}