Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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."


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...