Reputation: 3429
I'm having a problem with RenderTargetBitmap, in that I can't get it consistently grab an updated render after I change bound properties on a background thread.
Here is what I have:
// Update a property on an INotifyPropertyChanged view model
// This runs on a background thread
viewModel.SomeBoundProperty += 10;
// Flush dispatcher queue (from http://stackoverflow.com/a/2596035/612510)
_lcd.Dispatcher.Invoke(() => {}, System.Windows.Threading.DispatcherPriority.Loaded);
// Render the updated control
_lcd.Dispatcher.Invoke(() =>
{
_lcd.Measure(new System.Windows.Size(240, 160));
_lcd.Arrange(new System.Windows.Rect(0, 0, 240, 160));
_lcd.UpdateLayout();
_renderTarget.Render(_lcd);
}
Alas, approximately half the time I'm getting the render before the control is updated with the new value, and the other half it updates correctly.
From what I understand WPF dispatches property change notifications automatically to the UI thread. How can I ensure that these dispatched notifications are all processed before doing the render? This code works fine if I make sure SomeBoundProperty
is updated on the Dispatcher thread, but that is less than ideal for this particular application.
Any suggestions?
Upvotes: 2
Views: 483
Reputation: 3429
Some trial and error has me figuring that property change notifications are dispatched at the DispatcherPriority.Background
priority, so changing the flushing line to this:
// Flush dispatcher queue
_lcd.Dispatcher.Invoke(() => {}, System.Windows.Threading.DispatcherPriority.ContextIdle);
...looks like it fixed the problem. DispatcherPriority.ContextIdle
is one level below DispatcherPriority.Backgound
. The render now updates consistently every time.
Upvotes: 1