c# - Why is the dispatcher behaving this way when I pass a parameter? -
i'm using datagridview in wpf. in roweditending event, old row (before editing ends) , call method through dispatcher new row (after edit)
private void mygrid_roweditending(object sender, datagridroweditendingeventargs e) { datarowview oldrowview = e.row.item datarowview; datarow oldrow = oldrowview.row; //when put breakpoint before dispatcher called, oldrow has old row values. dispatcher.begininvoke(new action(() => onrowedit(oldrow, e)), system.windows.threading.dispatcherpriority.background); //i have passed old row onrowedit } void onrowedit(datarow oldrow, datagridroweditendingeventargs e) { //here oldrow has new row values. }
the item array of oldrow before call method , array after have called method don't match. reason behind this?
without a good, minimal, complete code example, it's impossible know sure issue in scenario. there's not enough context here.
but likely, you're running temporal issue. is, delegate invoked begininvoke()
executed asynchronously, @ indeterminate later time. it's queued execution, it's going executed after update of row occurs, hence see new values time method executed.
possible solutions include:
- use
invoke()
method instead ofbegininvoke()
. executed synchronously, ensuring row object hasn't been modified whenonrowedit()
method called. - if need values row, extract temporary object (e.g. new array) before calling
begininvoke()
, pass that object instead of row object itself. - just call
onrowedit()
directly. lacking complete code example, it's not clear why you're usingbegininvoke()
@ all; typically, user editing actions occur in dispatcher thread, , 1 can access ui objects directly, without need callinvoke()
orbegininvoke()
. note practically same option #1…it may usinginvoke()
superfluous.
if none of above options work you, please improve question including code example, explaining why using begininvoke()
in first place, , why none of these suggestions work in case.
Comments
Post a Comment