public static CancellationRegion SetNonCancelable()
{
CancellationSignal signal = new CancellationSignal(false);
Thread t = Thread.CurrentThread;
signal.Thread = t;
List<CancellationSignal> signals = t.CancellationSignals;
CancellationRegion region = new CancellationRegion(signal);
lock(signals) {
signals.Add(signal);
// Note that all failures due to allocations will be above
// this point in this method. Also, note that it's fine to
// Add first then get thread affinity later - the Cancel
// method holds this same lock while cancelling.
// Ensure that another fiber cannot run on this thread.
Thread.BeginThreadAffinity();
}
return region;
}