.net - How to apply TAP on a "partially" async method? -
i'll start explaining mean "partially" async:
have method a
bunch of things. 1 of these things calling method b
(fully) async. result, let a
return task b
, rest of a
in continuation.
now a
async method, before b
called still synchronous. according tap async method should minimal synchronous work before returning task.
what should in situation?
these options have considered:
- let whatever code calls
a
wrapa
in task.- just don't postfix
a
"async". - also don't
b
asynchronous either.
- just don't postfix
- make
a
asynchronous.
- postfix
a
async , wrap wholea
in task. - don't
b
asynchronous anymore becausea
's task wraps it.
- postfix
code:
base
public task basync() { return task.run(b); } private void b() { /* stuff.. */ }
option 1.1
public async task a() { // stuff.. await basync(); // stuff.. }
option 1.2
public void a() { // stuff.. b(); // stuff.. }
option 2.1
public task aasync() { return task.run(a); } private async task a() { // stuff.. await basync(); // stuff.. }
option 2.2
public task aasync() { return task.run(a); } private void a() { // stuff.. b(); // stuff.. }
i think basic principle synchronous methods should synchronous , asynchronous methods should asynchronous, there shouldn't between that.
so think option 2.1 best here.
one exception if had performance critical asp.net application. don't have ui thread there (that want keep free as possible). , might care performance penalty of running code asynchronously. in case, think option 1 appropriate (but behavior should documented).
Comments
Post a Comment