c# - WPF window image updating from menuitem but not when in while loop -
okay, real head-scratcher:
if select menuitem causes image, makes entire window (a writeablebitmap) have pixels drawn on it, , displays correctly.
however, if add while loop (let's 5 loops) same method drawing on bitmap not display until loop completed , 5th redrawn bitmap correctly displayed.
so, there sort of 'automatic refresh' happening window when menuitem selected being skipped in while loop?
more details. works fine (brings in 'clean' image, draws stuff on it, displays it):
// brings in 'clean' image writeablebitmap = new writeablebitmap(cleanvegmap); image.source = writeablebitmap; // makes bunch of draws on bitmap drawdinos2d(); this, however, 'goes away' 10 seconds , displays last (i.e. 5th) image:
int z = 0; while (z < 5){ z++; // brings in 'clean' image writeablebitmap = new writeablebitmap(cleanvegmap); image.source = writeablebitmap; // makes bunch of draws on bitmap drawdinos2d(); } new idea: possible somehow 5 'drawn' writeablebitmaps being cached in memory, somehow system?
tried using dispatcher (like below):
dispatcher.invoke((action)delegate { writeablebitmap = new writeablebitmap(cleanvegmap); image.source = writeablebitmap; drawdinos2d(); }); same thing (goes away 10 seconds , displays last image.
another clue: put messagebox in loop @ bottom of each loop and, somehow suspected, 'blitted' redrawn screen correctly. somehow:
system.windows.messagebox.show("glarp!"); this call 'woke up' system. again, ideas?
what happened when inserted messagebox processing , got results expecting ui thread had chance 'caught up' while messagebox open. created 'illusion' using messagebox made work, behind scenes threads sorting out , clearing instruction queues.
to create same effect programmatically, can update bitmap method (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 operation asynchronously on thread bitmap's dispatcher associated , give ui chance catch up. depending upon payload of 'drawdinos2d' method, might have migrate processing background thread , feed ui thread on piece-by-piece basis. start approach first.
eta: in .net 4.0 framework, counterpart above looks 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 specified delegate asynchronously specified arguments on thread system.windows.threading.dispatcher created on."
Comments
Post a Comment