System.Windows.Interop.D3DImage.Unlock C# (CSharp) Method

Unlock() public method

Unlocks the D3DImage Can only be called while locked. If you have dirtied the image with AddDirtyRect, Unlocking will trigger us to copy the dirty regions from the back buffer to the front buffer. While this is taking place, Lock will block. To avoid locking indefinitely, use TryLock.
public Unlock ( ) : void
return void
        public void Unlock()
        {
            WritePreamble();
            
            if (_lockCount == 0)
            {
                throw new InvalidOperationException(SR.Get(SRID.Image_MustBeLocked));
            }

            --_lockCount;

            if (_isDirty && _lockCount == 0)
            {
                SubscribeToCommittingBatch();
            }

            if (_isChangePending)
            {
                _isChangePending = false;
                
                WritePostscript();
            }
        }

Usage Example

        /// <summary>
        /// The render by OpenGL.
        /// </summary>
        private void RenderGLToD3dImage(D3DImage image, int w, int h, Action rendering)
        {
            if (w == 0 || h == 0)
            {
                return;
            }
            this.glControl.MakeCurrent();

            // resize D3D/OpenGL Surface if need
            this.ResizeIfNeed(w, h);

            // OnRender may be called twice in the same frame. Only render the first time.
            if (image.IsFrontBufferAvailable)
            {
                // render to sharedSurface using OpenGL
                this.wgl.WglDXLockObjectsNV(wglHandleDevice, 1, singleWglHandleSharedSurfaceArray);
                GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo);
                GL.DrawBuffer((DrawBufferMode)FramebufferAttachment.ColorAttachment0);
                rendering();
                GL.Finish();
                this.wgl.WglDXUnlockObjectsNV(wglHandleDevice, 1, singleWglHandleSharedSurfaceArray);

                try
                {
                    image.Lock();
                    image.SetBackBuffer(D3DResourceType.IDirect3DSurface9, sharedSurface.NativePointer);
                    image.AddDirtyRect(new Int32Rect(0, 0, w, h));
                }
                catch (Exception ex)
                {
                    // ???
                    Console.WriteLine(ex.ToString());
                    this.device.ResetEx(ref this.presentparams);
                }
                finally
                {
                    image.Unlock();
                }
            }
        }