WriteableBitmap.Lock Metodo

Definizione

Riserva il buffer nascosto per gli aggiornamenti.

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

Esempio

Nell'esempio di codice seguente viene illustrato come riservare il buffer nascosto usando il Lock metodo .

    // 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();
        }
    }

Commenti

Il Lock metodo incrementa il conteggio dei blocchi. Quando un oggetto WriteableBitmap è bloccato, il sistema di rendering non invia aggiornamenti fino a quando non WriteableBitmap viene sbloccato completamente dalle chiamate al Unlock metodo .

È possibile usare il Lock metodo per supportare implementazioni multithread. In questi scenari, il thread dell'interfaccia utente blocca la bitmap ed espone il buffer nascosto ad altri thread. Al termine di un frame, il thread dell'interfaccia utente aggiunge rettangoli modificati e sblocca il buffer.

Il thread dell'interfaccia utente può bloccarsi quando il thread di rendering acquisisce un blocco sul buffer nascosto per copiarlo in avanti nel buffer anteriore. Se la latenza da questo blocco è troppo lunga, usare il TryLock metodo per attendere un breve periodo di tempo e quindi sbloccare il thread dell'interfaccia utente per eseguire altre attività mentre il buffer nascosto è bloccato.

Si applica a

Vedi anche