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

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -