System.Runtime.InteropServices.SafeHandle.Close C# (CSharp) Method

Close() private method

private Close ( ) : void
return void
		public void Close ()
		{
			if (refcount == 0)
				throw new ObjectDisposedException (GetType ().FullName);

			int newcount = 0, current = 0;
			bool registered = false;
			RuntimeHelpers.PrepareConstrainedRegions ();
			try {
				do {
					current = refcount;
					newcount = current-1;

					// perform changes in finally to avoid async interruptions
					try {}
					finally {
						if (Interlocked.CompareExchange (ref refcount, newcount, current) == current)
							registered = true;
					}
				} while (!registered);
			} finally {
				if (registered && newcount == 0 && owns_handle && !IsInvalid){
					ReleaseHandle ();
					handle = invalid_handle_value;
					refcount = -1;
				}
			}
		}

Usage Example

 public static void ClosePortable(SafeHandle handle)
 {
     handle.Close();
 }