Share via


WriteableBitmap.Lock 메서드

정의

백 버퍼 업데이트를 예약합니다.

public:
 void Lock();
public void Lock ();
member this.Lock : unit -> unit
Public Sub Lock ()

예제

다음 코드 예제에서는 메서드를 사용하여 백 버퍼를 예약하는 방법을 보여 있습니다 Lock .

    // The DrawPixel method updates the WriteableBitmap by using
    // unsafe code to write a pixel into the back buffer.
    static void DrawPixel(MouseEventArgs e)
    {
        int column = (int)e.GetPosition(i).X;
        int row = (int)e.GetPosition(i).Y;

        try{
            // Reserve the back buffer for updates.
            writeableBitmap.Lock();

            unsafe
            {
                // Get a pointer to the back buffer.
                IntPtr pBackBuffer = writeableBitmap.BackBuffer;

                // Find the address of the pixel to draw.
                pBackBuffer += row * writeableBitmap.BackBufferStride;
                pBackBuffer += column * 4;

                // Compute the pixel's color.
                int color_data = 255 << 16; // R
                color_data |= 128 << 8;   // G
                color_data |= 255 << 0;   // B

                // Assign the color data to the pixel.
                *((int*) pBackBuffer) = color_data;
            }

            // Specify the area of the bitmap that changed.
            writeableBitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));
        }
        finally{
            // Release the back buffer and make it available for display.
            writeableBitmap.Unlock();
        }
    }

설명

메서드는 Lock 잠금 수를 증분합니다. 가 WriteableBitmap 잠겨 있으면 렌더링 시스템은 메서드 호출에 의해 Unlock 가 완전히 잠금 해제될 때까지 WriteableBitmap 업데이트를 보내지 않습니다.

메서드를 Lock 사용하여 다중 스레드 구현을 지원할 수 있습니다. 이러한 시나리오에서 UI 스레드는 비트맵을 잠그고 백 버퍼를 다른 스레드에 노출합니다. 작업자 스레드가 프레임을 완료하면 UI 스레드는 변경된 사각형을 추가하고 버퍼의 잠금을 해제합니다.

렌더링 스레드가 백 버퍼에 대한 잠금을 획득하여 전면 버퍼로 복사할 때 UI 스레드가 차단할 수 있습니다. 이 블록의 대기 시간이 너무 긴 경우 메서드를 사용하여 TryLock 짧은 시간 동안 기다린 다음, 백 버퍼가 잠겨 있는 동안 UI 스레드의 차단을 해제하여 다른 작업을 수행합니다.

적용 대상

추가 정보