Featured post
c# - How to start a thread to keep GUI refreshed? -
i have window button triggers lengthy processing. put processing in separate thread, -- surprise -- makes gui frozen anyway. no control refreshed, cannot move window.
so the question how start thread, won't interfere gui, i.e. gui date (while processing change data, , gui displays pieces of it)?
that how start thread currectly:
var thread = new thread(dolearn); thread.isbackground = true; thread.start();
edit 1
jon:
- i don't use locks @ all
- no join calling
- the ui thread left alone -- sits there
the processing big loop math operations, not allocating memory, on ui side have controls binding (wpf) data, number of current iteration of main loop. should refreshed each time main loop "ticks". counter of loop property triggers onpropertychanged each change (classic wpf binding).
edit 2 -- there!
ok, jon hit nail @ head (who surprises? ;-d) -- thank you! problem comes changing counter. when used instead counter, local counter gui refreshed -- mean move windows, but... couldn't see display of counter.
what have here -- wpf gui, such data-binding
<textblock text="{binding path=counter"/>
and have counter property of course on each change sends event propertychanged. 1 of listeners sure gui.
so, jon answer valid "the answer", design pov not exactly, because if gui part has pull info counter , update display every (let's say) 3 seconds, why use data binding? me such approach invalidates data binding idea.
i could, theoretically, pass processing thread gui dispatcher, , sending in gui thread, , work (i didn't try it) mean tight coupling of non-gui part , gui part.
so far, have no idea how "right" way. best guess far create timerdispatcher not @ gui side inside processing library, , update counter value sending time time (i didn't try yet though).
small remark: have more properties binded actually, isrunning, changed @ beginning , @ end of processing. , changes affect display correctly -- counter change triggers around 3000 notifications in 3-4 seconds. looks jamming problem. did test -- killed data binding partially, notifications sent, gui not "receiving" them -- listening them. in such case gui frozen.
so, still listening advices -- thank advance sharing.
edit 3
the saga continues here:
it should fine is. things may freezing ui:
- are locking within ui thread, , locking on same lock in other thread?
- are calling
join
on thread ui thread? - are doing other heavy work in ui thread?
if come short complete program shows problem, i'm sure fix it... should okay.
edit: okay, you've added this:
the counter of loop property triggers onpropertychanged each change (classic wpf binding).
so you're updating property non-ui thread? expect cause problems, because trigger ui changes wrong thread.
i suggest take approach such as:
- periodically update counter via
dispatcher.begininvoke
- have "ui counter" , "worker counter" - , copy value "worker counter" "ui counter" in ui thread via
dispatchertimer
, polling it.
- Get link
- X
- Other Apps
Comments
Post a Comment