c# - Implication of calling result on awaited task -
given have kind of rudimentary async cqrs setup below:
public interface irequest<treturn> { } public interface irequesthandler<trequest, treturn> trequest : irequest<treturn> { task<treturn> requestasync<trequest>(trequest request); } public class getthings : irequest<string[]> { } public class getthingshandler : irequesthandler<getthings, string[]> { public async task<string[]> requestasync<trequest>(trequest request) { await task.run(() => thread.sleep(200)); return new[] {"tim", "tina", "tyrion"}; } }
and have kind of dependency injection container:
public class container { public object resolve(type servicetype) { var possibletypes = new dictionary<type, object>(); possibletypes.add(typeof(getthings), new getthingshandler()); return possibletypes[servicetype]; } }
for our 'requestbus' implementation, generic type parameter information lost, due being on other end of rest style service:
public async task<object> getrequest(object o) { type t = o.gettype(); object handler = container.resolve(t); var methodinfo = handler.gettype().getmethod("requestasync"); methodinfo = methodinfo.makegenericmethod(typeof (object)); task methodinvocationtask = (task)methodinfo.invoke(handler, new[] {o}); await methodinvocationtask; // or: return ((dynamic) methodinvocationtask).result; return methodinvocationtask.gettype().getproperty("result").getvalue(methodinvocationtask); }
the thing known tasks may return object , can deduce type of request , request return type (it treated known here simplicity).
the question is: calling await
on (void) task
before asking task.result
executing in async way? assuming handler actual async work method going async?
(note: these simplified implementations sake of asking question)
if await
task rest of method executed continuation of task
, control returned caller of current method, yes, it'll asynchronous.
that point of await
after all; behave asynchronously.
calling result
on completed task
isn't problem. will block current thread until task
has completed, know it's completed, that's not problem.
Comments
Post a Comment