.net - The proper way to await databound property getters c# -
what correct way use async method in databound property getter? talking solid, scientific arguments, not personal preferences. i've read many threads problem, not specific case. of solutions don't work in cases, , of suggestion, well... subjective or wrong.
what don't accept , why:
- you can't - actually, possible. there many posts "there no such things async properties", "it against design of language" etc. there many sensible explanations why such expressions false
- this should me method, not property - can't be. want databind it. provide property "proxies" people using code because in future there may different method calculate pseudo-property. , want view-side of binding simple possible
- use property store cached result of method - defeat purpose, changes dynamically , class orm entity store redundant data db.
- use
sometask.result;
orsometask.getawaiter().getresult()
- in cases use it. i've used in many cases i.e. console applications. it's nice, clear , readable. when use in databound property deadlock
problem background (simplified)
let's responsible developing orm mechanism in project. there first stable version, want add properties entities
databinders responsible layout. can edit entity
layer, can't edit mapping
, repository
layers. (i not held againt will, situation fictional simplification). methods in repositories async. can ask responsible provide identical synchronous methods all of methods, stupid kind of redundant work.
only solution can use now
_something = task.run(async () => await anotherrepository.calculatestuff(this)).result;
and doesn't right me. works, have await method inside lambda in task.run()
. stuck time being, , want know simplest , correct approach.
repository method pseudo-code
public async static task<ilist<calculatedstuff>> calculatestuff(someclass class) { return await task.run(() => { using (var session = helper.opensession()) return session.createcriteria(typeof(calculatedstuff)).add(restrictions.eq("someclass", class)) ///... .list<calculatedstuff>(); }); }
if have access source code of anotherrepository.calculatestuff
, can implement in way won't deadlock when called bound property. first short summary of why deadlocks. when await something, current synchronization context remembered , rest of method (after async) executed on context. ui applications means rest of method executed on ui thread. in case ui thread blocked waiting result
of task - hence deadlock.
but there method of task
named configureawait
. if pass false
it's argument (named continueoncapturedcontext
) , await task returned method - won't continue on captured context, solve problem. suppose have:
// ui bound public string data { { return getdata().result; } } static async task<string> getdata() { await task.run(() => { thread.sleep(2000); }); return "test!"; }
this deadlock when called ui thread. if change it:
static async task<string> getdata() { await task.run(() => { thread.sleep(2000); }).configureawait(false); return "test!"; }
it won't more.
for might read later - don't way, if temporary debugging purposes. instead return dummy object property getter isloading flag set true, , meanwhile load data in background , fill dummy object properties when done. not freeze ui during long blocking operation.
Comments
Post a Comment