WPF window image updating from menuitem but not when in while loop

家住魔仙堡 提交于 2019-11-28 10:32:31

问题


Okay, this is a real head-scratcher:

If I select a menuitem that causes the image, that makes up the entire window (a writeableBitmap) to have some pixels drawn on it, it does so and displays correctly.

However, if I add a while loop (let's say for 5 loops) to the same method the drawing on the bitmap DOES NOT DISPLAY until the loop is completed and then the 5th redrawn bitmap is correctly displayed.

So, is there some sort of 'automatic refresh' that is happening to the window when a menuitem is selected but is being skipped in the while loop?

More details. This works fine (brings in a 'clean' image, draws some stuff on it, displays it):

// This brings in a 'clean' image
writeableBitmap = new WriteableBitmap(CleanVegMap);
image.Source = writeableBitmap;
// This makes a bunch of draws on the bitmap
DrawDinos2d();

This, however, 'goes away' for 10 seconds and then only displays the last (i.e. 5th) image:

int z = 0;
while (z < 5){
z++;
   // This brings in a 'clean' image
   writeableBitmap = new WriteableBitmap(CleanVegMap);
   image.Source = writeableBitmap;
   // This makes a bunch of draws on the bitmap
   DrawDinos2d();
}

New idea: is it possible that somehow the 5 'drawn' writeableBitmaps are being cached in memory, somehow by the system?

Tried using the Dispatcher (like below):

                Dispatcher.Invoke((Action)delegate
            {               
                writeableBitmap = new WriteableBitmap(CleanVegMap);
                image.Source = writeableBitmap;
                DrawDinos2d();
            });

Same thing (goes away for 10 seconds and then displays only the last image.

Another clue: I just put a MessageBox in the loop at the bottom of each loop and, as I somehow suspected, it 'blitted' the redrawn screen correctly. Somehow:

 System.Windows.MessageBox.Show("Glarp!");

this call 'woke up' the system. Again, any ideas?


回答1:


What happened when you inserted a MessageBox into the processing and got the results you were expecting was that the UI thread had a chance to get 'caught up' while the MessageBox was open. So it created the 'illusion' that using an MessageBox suddenly made it work, but behind the scenes it was just threads sorting themselves out and clearing their instruction queues.

To create the same effect programmatically, you can update your bitmap with a method like this (ETA: requires .NET 4.5 Framework)...

    public void UpdateBitmap()
    {
        WriteableBitmap writeableBitmap = new WriteableBitmap
                                (100, 100, 96, 96, PixelFormats.Bgr32, null);
        writeableBitmap.Dispatcher.InvokeAsync(() =>
            {
                Console.WriteLine("work goes here");
            });
    }

This runs the operation asynchronously on the thread that the bitmap's dispatcher is associated with and will give the UI a chance to catch up. Depending upon the payload of your 'DrawDinos2d' method, you MIGHT have to migrate the processing to a background thread and feed it to the UI thread on a piece-by-piece basis. But start with this approach first.

ETA: In a .NET 4.0 framework, the counterpart to the above looks like this...

    public void UpdateBitmap()
    {
        object[] objs = new object[] {null};
        WriteableBitmap writeableBitmap = new WriteableBitmap(
             100, 100, 96, 96, PixelFormats.Bgr32, null);
        writeableBitmap.Dispatcher.BeginInvoke((SendOrPostCallback)delegate
            {
                Console.WriteLine(@"work goes here");
            }, objs);
    }

The docs read "Executes the specified delegate asynchronously with the specified arguments on the thread that the System.Windows.Threading.Dispatcher was created on."



来源:https://stackoverflow.com/questions/17728671/wpf-window-image-updating-from-menuitem-but-not-when-in-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!