System.Runtime.InteropServices.SafeHandle.DangerousAddRef C# (CSharp) Méthode

DangerousAddRef() private méthode

private DangerousAddRef ( bool &success ) : void
success bool
Résultat void
		public void DangerousAddRef (ref bool success)
		{
			if (refcount <= 0)
				throw new ObjectDisposedException (GetType ().FullName);

			bool registered = false;
			int newcount, current;
			do {
				current = refcount;
				newcount = current + 1;
				
				if (current <= 0){
					//
					// In MS, calling sf.Close () followed by a call
					// to P/Invoke with SafeHandles throws this, but
					// am left wondering: when would "success" be
					// set to false?
					//
					throw new ObjectDisposedException (GetType ().FullName);
				}

				// perform changes in finally to avoid async interruptions
				RuntimeHelpers.PrepareConstrainedRegions ();
				try {}
				finally {
					if (Interlocked.CompareExchange (ref refcount, newcount, current) == current)
						registered = success = true;
				}
			} while (!registered);
		}

Usage Example

        internal void SetParent(SafeHandle parent)
        {
            bool addedRef = false;
            parent.DangerousAddRef(ref addedRef);
            Debug.Assert(addedRef);

            _parent = parent;
        }
All Usage Examples Of System.Runtime.InteropServices.SafeHandle::DangerousAddRef