c# - update wpf datagrid cell from other thread -
i'm trying implement simple spreadsheet using wpf datagrid. when users input formula in cell, engine calculates formula , update cell calculated value. of calculations handle take long time, want calculate in other threads.
i tried implement using celleditending event below. (the itemssource of datagrid1 bound datatable dtab.)
private void datagrid1_celleditending(object sender, datagridcelleditendingeventargs e) { action calc = () => { int r = datagrid1.items.indexof(e.row.item); int c = datagrid1.columns.indexof(e.column); dtab.rows[r].setfield(c, "x"); // calculated value }; action update = () => { if (datagrid1.dispatcher.checkaccess()) calc(); else datagrid1.dispatcher.begininvoke(calc); }; //update(); // run same ui thread task.factory.startnew(update); // run different thread }
when update cell value same ui thread, cell updated value "x", different thread, not. how can update cell value "x" different thread?
edit: code not throw invalidoperationexception bcz used dispatcher.begininvoke(). problem seems user input value (formula) overwrites calculated value want display.
you cannot update gui thread. fortunately .net provides mechanism handle this:
uicontrol.**dispatcher**.invoke(delegate)
for example
datagrid1.dispatcher.invoke(() => myupdatefunction(datagrid1, data))
edit: sorry original code windows.forms
Comments
Post a Comment