internal LockBase(AcquireIntTimeoutMethod acquire, ReleaseMethod release, int timeout) { this.release = release; try { acquire(timeout); } catch (ApplicationException) { timedOut = true; } }
internal LockBase(AcquireTimeSpanTimeoutMethod acquire, ReleaseMethod release, TimeSpan timeout) { this.release = release; try { acquire(timeout); } catch (ApplicationException) { timedOut = true; } }
private IntPtr Initialize(Func <object> createInnerObject) { try { // implement IUnknown methods _queryInterfaceMethod = delegate(IntPtr pUnk, ref Guid iid, out IntPtr ppv) { lock (this) { // delegate anything but IID_IUnknown to the aggregated object if (IID_IUnknown == iid) { ppv = _outerObject; Marshal.AddRef(_outerObject); return(S_OK); } return(Marshal.QueryInterface(_aggregatedObject, ref iid, out ppv)); } }; _addRefMethod = delegate(IntPtr pUnk) { lock (this) { return(++_refCount); } }; _releaseMethod = delegate(IntPtr pUnk) { lock (this) { if (0 == --_refCount) { Free(); } return(_refCount); } }; // create the IUnknown vtable var vtable = new UnkVtable(); vtable.pQueryInterface = Marshal.GetFunctionPointerForDelegate(_queryInterfaceMethod); vtable.pAddRef = Marshal.GetFunctionPointerForDelegate(_addRefMethod); vtable.pRelease = Marshal.GetFunctionPointerForDelegate(_releaseMethod); _pVtable = Marshal.AllocCoTaskMem(Marshal.SizeOf(vtable)); Marshal.StructureToPtr(vtable, _pVtable, false); // create the IUnknown object var unkObject = new UnkObject(); unkObject.pVtable = _pVtable; _outerObject = Marshal.AllocCoTaskMem(Marshal.SizeOf(unkObject)); Marshal.StructureToPtr(unkObject, _outerObject, false); // pin the managed ComWrapper instance _gcHandle = GCHandle.Alloc(this, GCHandleType.Normal); // create and aggregate the inner object _aggregatedObject = Marshal.CreateAggregatedObject(_outerObject, createInnerObject()); return(_outerObject); } catch { Free(); throw; } }