c# - Dispatcher Task /Action Completion -
i have application fetches info form server , gives result user . info beingness fetched quite big blocks ui sometime. using dispatcher create asynchronous.
here code snippet :-
private void getdata(object sender, routedeventargs e) { list<result> data=new list<result>; dispatcherobject obj= dispatcher.begininvoke(dispatcherpriority.background,(threadstart)delegate { info =data.fetch_data(id, name, url); }); if(obj.completed){ messagebox.show("done!"); } }
but code gives error saying
"cannot implicitly convert type 'system.windows.threading.dispatcheroperation' 'system.windows.threading.dispatcherobject' ".
is there anyway user can notified when background task gets completed?
edit :-
here async/await code
private async void getdata(object sender, routedeventargs e) { task<list<result>> t =task<list<resultsummary>>.factory.startnew(() => data==data.fetch_data(id, name, url)); await t; }
but gives error "" calling thread cannot access object because different thread owns it."
calling dispatcher.begininvoke
not enable perform asynchronous actions on background thread unless initialised dispatcher
object on background thread. so, instead of using dispatcher
want, in order fetch info on background thread , pass info background thread ui thread, can utilize task
class.
task.factory.startnew((func<yourdatatype>)delegate() { // fetch info on background thread here (return yourdatatype, whatever is) homecoming dataaccessclass.getdata(); }).continuewith((task<yourdatatype> task) => { // update controls result on ui thread here youruiproperty = task.result; }, taskscheduler.fromcurrentsynchronizationcontext());
obviously, you'll need replace yourdatatype
type whatever info type is.
c# wpf dispatcher
No comments:
Post a Comment