wpf - C# Update UI In Task -
i new c# task , threading.
i have code below:-
public void updatesales(object sender, eventargs args) { task.run(() => { // code create collection ... // code business logic .. // below code update ui // safe update ui below saledatagrid.dispatcher.invoke((action) (() => { saledatagrid.itemssource = currentcollection; saledatagrid.items.refresh(); })); }); } i not sure if code correct or not. think in case deadlock can occur?
can please point how can update ui task? not using async/await because updatesales event handler third party library.
assuming updatesales called on ui thread, cleaner solution this:
public async void updatesales() { var collection = await task.run(() => { // code create collection ... // code business logic .. return currentcollection; }); saledatagrid.itemssource = collection; saledatagrid.items.refresh(); } as describe on blog, await automatically resume on captured context (in case, ui context). prefer using implicit context of await rather dispatcher directly: code shorter , more portable.
Comments
Post a Comment