internal SuspendToken(SuspendableObject parent) { var suspendLevel = System.Threading.Interlocked.Increment(ref parent._suspendLevel); _parent = parent; if (1 == suspendLevel) { try { _parent.OnSuspended(); } catch (Exception) { System.Threading.Interlocked.Decrement(ref parent._suspendLevel); _parent = null; throw; } } }
internal void ResumeTemporarily() { Exception ex1 = null; Exception ex2 = null; Exception ex3 = null; // Try to bring the suspend level to 0 int suspendLevel = _parent._suspendLevel; while (suspendLevel != 0) { if (suspendLevel > 0) { if (suspendLevel == 1) { try { _parent.OnAboutToBeResumed(); } catch (Exception ex) { ex1 = ex; } } suspendLevel = System.Threading.Interlocked.Decrement(ref _parent._suspendLevel); ++_numberOfSuspendLevelsAbsorbed; if (suspendLevel == 0) { try { _parent.OnResume(); } catch (Exception ex) { ex2 = ex; } } } else if (suspendLevel < 0) { suspendLevel = System.Threading.Interlocked.Increment(ref _parent._suspendLevel); --_numberOfSuspendLevelsAbsorbed; if (suspendLevel == 1) { try { _parent.OnSuspended(); } catch (Exception ex) { ex3 = ex; } } } } if (null != ex1) { throw ex1; } if (null != ex2) { throw ex2; } if (null != ex3) { throw ex3; } }