Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Bug: Dispatcher and WriteableBitmap

$
0
0

I think I found a bug. It is a little complicated to set up, but here goes.
Have only tested on Windows 8.

Create new WPF project.
Insert an Image control and name it "ImageControl".
Insert following code:

private WriteableBitmap awesomeData = new WriteableBitmap(10, 10, 96, 96, PixelFormats.Bgr32, null);

public MainWindow()
{
    InitializeComponent();
    SizeChanged += MainWindow_SizeChanged;
    Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)SomeMethod);
}

void SomeMethod()
{
    WriteableBitmap local = awesomeData;
    local.Lock();

    Thread.Sleep(10);

    if (local != awesomeData)
    {
        MessageBox.Show("Bug!");
        Application.Current.Shutdown();
        return;
    }

    awesomeData.AddDirtyRect(new Int32Rect(0, 0, awesomeData.PixelWidth, awesomeData.PixelHeight));
    awesomeData.Unlock();

    Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)SomeMethod);
}

void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
    awesomeData = new WriteableBitmap(1000, 1000, 96, 96, PixelFormats.Bgr32, null);
    ImageControl.Source = awesomeData;
}

Now run the program, and try to maximize/restore it using (WIN)+UP/(WIN)+DOWN shortcuts.
It can take some time to trigger (64-bit recommended as GC may not be able to keep up).

Now, as far as I understand, events and Dispatcher items are handled on the UI thread.
Therefore both methods MainWindow_SizeChanged and SomeMethod() should execute sequentially.

Apparently they don't, and MainWindow_SizeChanged can run while the UI thread is sleeping/waiting.

Or am I missing something?

Best regards,
Alexei Mihalchuk


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>